Read text file without header and separate columns

Dear Matlab Team,
my problem is as follows. I could not find a solution in other forums, but none seem to work for my issue:
I have a text file with a big headliner, which I want to skip. The data I need starts in line 194 and has 50.000 values. There are 3 values in each row, sperated by ",". Basically, what I need is to extract the 50 000 values and put it in matrix with 3 columns for further analyzation.
This is how the file looks like (starting from below "DATA BLOCK..."]
...
[DATA BLOCK 1-1-1]
0.00000,20.47668,0.13959
0.00010,20.46021,0.16467
0.00020,20.47668,0.11914
0.00030,20.48767,0.19208
...
How can I separate this data and create a matrix from it?
Thanks in advance,
Philipp

 採用された回答

Rik
Rik 2020 年 4 月 29 日
編集済み: Rik 2020 年 4 月 29 日

1 投票

You can use the readmatrix function.
A = readmatrix(filename,'NumHeaderLines',193);
Edit:
As Walter suggested below: releases older than R2019a are missing the readmatrix function. On R2013b or newer, the readtable function can help out:
A = table2array(readtable(filename,'NumHeaderLines',193, 'readvariablenames', false));

8 件のコメント

Philipp Reh
Philipp Reh 2020 年 4 月 29 日
Thanks for your quick response.
It does not recognize the "readmatrix" command; "Undefinded function or variable 'readmatrix'. (Spelling is correct).
Is there any code beforehand necessary and do the "," have no influence?
Walter Roberson
Walter Roberson 2020 年 4 月 29 日
You did not specify your MATLAB release. readmatrix() needs R2019b or later.
For earlier releases,
A = table2array(readtable(filename,'NumHeaderLines',193, 'readvariablenames', false));
readtable() should be able to detect the comma delimiter. If you want to be sure, you can use
'Delimiter', ','
Philipp Reh
Philipp Reh 2020 年 4 月 29 日
I downloaded the 2020a version and it worked (was 2018).
Thanks a lot.
Philipp
Philipp Reh
Philipp Reh 2020 年 5 月 5 日
I have a follow up question, which is a bit off topic. I hope you can still help me. I have more than one txt file in the main file. I want to add the other files to the same array, so that I have a 50.000 (rows) x 90 (columns) (for 30 files) Matrix. I have the main code ready, however I don't know how it adds up all three columns and not only one:
input_folder = 'folder';
files = dir(fullfile(input_folder, '*.txt'));
file_paths = fullfile({files.folder}, {files.name});
for i = 1 : numel(file_paths)
data = readmatrix(file_paths{i},'NumHeaderLines',193);
A(:,i) = data(:,3);
end
I think the Mistake is in the last line, can you help me here again please?
Best,
Philipp
Rik
Rik 2020 年 5 月 5 日
Assuming you have 3 columns per file, something like the code below should help. The point is that you need to compute the indices
cols=3*(i-1)+(1:3);
A(:,cols)=data;
Walter Roberson
Walter Roberson 2020 年 5 月 6 日
sum_by_3 = squeeze(sum(reshape(data, size(data,1), 3, []),2));
This sums groups of 3 columns, so like
[data(1,1)+data(1,2)+data(1,3), data(1,4)+data(1,5)+data(1,6), ...
data(2,1)+data(2,2)+data(2,3), data(2,4)+data(2,5)+data(2,6), ...
Walter Roberson
Walter Roberson 2020 年 5 月 6 日
Looks like I misinterpreted what was being requested.
You should be pre-initializing A.
input_folder = 'folder';
files = dir(fullfile(input_folder, '*.txt'));
file_paths = fullfile({files.folder}, {files.name});
nfile = numel(file_paths);
for i = 1 : nfile
data = readmatrix(file_paths{i},'NumHeaderLines',193);
if i == 1
A(:,3*nfile) = 0; %grow array to largest size
else
A(:, i*3-2:i*3) = data;
end
end
Philipp Reh
Philipp Reh 2020 年 5 月 6 日
Thank you.

サインインしてコメントする。

その他の回答 (1 件)

B
B 2022 年 4 月 21 日
編集済み: Rik 2022 年 4 月 21 日

0 投票

I think summarizing you've got something like this:
input_folder = 'folder';
files = dir(fullfile(input_folder, '*.txt'));
file_paths = fullfile({files.folder}, {files.name});
nfile = numel(file_paths);
for i = 1 : nfile
data= readmatrix(file_paths{i},'NumHeaderLines',1);% a display of data in a single file
cols=3*(i-1)+(1:3)
A(:,cols)=data; % A is your desired result outpout file
end

カテゴリ

ヘルプ センター および File ExchangeLarge Files and Big Data についてさらに検索

質問済み:

2020 年 4 月 29 日

編集済み:

Rik
2022 年 4 月 21 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by