MATLAB Answers


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

Asked by Surush
on 27 Feb 2014
Latest activity Commented on by Surush
on 27 Feb 2014
Accepted Answer by Sven

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:

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
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:

tline = fgetl(fid);
while ischar(tline)
    tline = fgetl(fid);

I've also tried to use this suggestion, but had no luck using it to handle my data:

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! :)



No products are associated with this question.

1 Answer

Answer by Sven
on 27 Feb 2014
Edited by Sven
on 27 Feb 2014
 Accepted answer

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.

tline = fgetl(fid);
tlines = cell(0,1);
while ischar(tline)
    tlines{end+1,1} = tline;
    tline = fgetl(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);
% 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));

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 *

Did that answer the question for you Surush?


on 27 Feb 2014

Thanks Sven!

Just one more question - each "block" in my text file is separated by an empty line, how would I be able to tell the program to disregard that empty line, and just move onto the next block?

Thanks again! :)

on 27 Feb 2014

If you know you want to ignore all empties, perhaps you could just do:

if ~isempty(tline)
  tlines{end+1,1} = tline;
on 27 Feb 2014

Brilliant. Thanks Sven!

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today