Reading lines from a text file and storing them into an array.

조회 수: 114 (최근 30일)
Surush
Surush 2014년 2월 27일
댓글: Adeline War 2023년 5월 18일
I have some "look-up table (LUT)" data within a text file, where each LUT is a block that has a single string (chemical reaction) at the top, and then two columns where there is a float in one column and an exponential in the second, as shown below:
e+CO2->e+CO2
0 1.057e-07
0.1 1.057e-07
0.2 9.04157e-08
0.3 8.33528e-08
0.4 7.7605e-08
0.5 7.27688e-08
e+CO2->CO2p+e+e
0 0
0.1 0
0.2 0
0.3 0
0.4 0
0.5 1.64069e-21
I need to write a program which reads each line, and stores each line into an array to be used later. Additionally, I need to cut out the first column of numbers from each of the LUT blocks (i.e. the 0, 0.1, 0.2, 0.3, 0.4, 0.5). However, I am finding it difficult to just read the data and put it into an array.
I have tried to use a simple fgetl program, but I don't know how to save each line into an array:
fid=fopen('LUT.txt');
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
fclose(fid);
I've also tried to use this suggestion, but had no luck using it to handle my data: http://www.mathworks.co.uk/help/matlab/import_export/import-text-data-files-with-low-level-io.html#f5-6402
The main difficulty I'm having is specifying the first line from each "block" (the chemical equation), and then specifying the format of all the data within each block.
Any suggestions would be greatly appreciated! :)

채택된 답변

Sven
Sven 2014년 2월 27일
편집: Sven 2014년 2월 27일
Hi Surush,
The trick is to put each line in an element of a cell array.
Here's some commented code that does what you're trying to do. After putting lines into a cell array, I use a regular expression to pick out which lines were equations and which were not.
fid=fopen('LUT.txt');
tline = fgetl(fid);
tlines = cell(0,1);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
end
fclose(fid);
% Find the tlines with equations on them
eqnLines = regexp(tlines,'.*->.*','match','once');
eqnLineMask = ~cellfun(@isempty, eqnLines);
% Convert the non equation lines to the second numeric value
for i = find(~eqnLineMask)'
bothNumbers = str2num(tlines{i});
tlines{i} = bothNumbers(2);
end
% Make blocks with the equation in the first cell
blocks = cell(nnz(eqnLineMask),2);
blocks(:,1) = tlines(eqnLineMask);
% And the numbers in an array in the second cell
eqnLineNos = [find(eqnLineMask); length(tlines)+1];
for i = 1:length(eqnLineNos)-1
inds = eqnLineNos(i)+1 : eqnLineNos(i+1)-1;
blocks{i,2} = cell2mat(tlines(inds));
end
So you end up with:
>> blocks
blocks =
'e+CO2->e+CO2' [6x1 double]
'e+CO2->CO2p+e+e' [6x1 double]
>> blocks{1,2}
ans =
1.0e-06 *
0.1057
0.1057
0.0904
0.0834
0.0776
0.0728
Did that answer the question for you Surush?
  댓글 수: 5
Agustín Santana Romero
Agustín Santana Romero 2021년 5월 25일
Hi Sven! I´m trying to do something similar but instead of reading a .txt, i´m reading a serialport. From arduino I´m sending some values through serial in two columns, first a column with n values (in this example 3) from the first sensor and a second column from the 2nd sensor (I mean, first one, and then the other, not together) so then appear some blank spaces like this:
90
80
70
10
20
30
60
50
40
40
50
60
I was trying to read all this data first like a table and then process it on Matlab (App Designer), but with readline I think is not possible, I mean, I´m not able to read alternately the first, and then the second column, removing blank spaces in order to graph each row separately.
Hope you or anybody to can read this.
Thank you!
Adeline War
Adeline War 2023년 5월 18일
@Sven hello Sven . I want to read each line and save it in a variable x and then read second line and replace the variable with new values. How do i do that?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by