Reading lines from a text file and storing them into an array.
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! :)
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!