MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by Surush on 27 Feb 2014

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

*No products are associated with this question.*

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.

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?

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

Sven 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; end

## 0 Comments