Reading lines from a text file and storing them into an array.
56 views (last 30 days)
Show older comments
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! :)
0 Comments
Accepted Answer
Sven
on 27 Feb 2014
Edited: Sven
on 27 Feb 2014
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 Comments
Agustín Santana Romero
on 25 May 2021
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
on 18 May 2023
@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?
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!