Search code examples
matlabdelimitertextscan

import data from a text file in MATLAB with multiple delimiters


I try to import data from a text file to MATLAB, which has the following structure:

** Porosity
**
*POR *ALL
0.1500 0.0900 2*0.1300 0.1400 4*0.1500 0.2200 2*0.1500 0.0500
0.0900 0.1400 5*0.1500 0.2300 0.2600 0.0800 0.1500 0.1500 0.2400 0.1700
[...]

The header has to be ignored obviously. Space is the delimiter, while * indicates that the same value occurs several times as indicated by the integer before the *.

Unfortunately, the number of entries per line varies. Ideally I want to store all values in one array like this:

por = [0.1500 0.0900 0.1300 0.1300 0.1400 0.1500 0.1500 0.1500 0.1500 0.1500 0.2200 0.1500 0.1500 ...]

Can this be solved with the textscan command somehow? The file is rather large with some hundred thousand values, so I need a quick solution ;) Help is greatly appreciated!


Solution

  • Straight forward way (I did not use Matlab for a long period of time, so it might be not the best solution)

    fid = fopen('temp.txt');
    data = textscan(fid, '%s', 'delimiter', ' ');
    fclose(fid);
    
    out = convert_cells(data);
    

    And function

    function out = convert_cells(cells)
      out = [];
      for i = 1 : size(cells{1})
         tmp = strsplit(cells{1}{i}, '*');
         num1 = str2double(tmp(1));
         if size(tmp, 2) == 2 && ~isnan(num1)
             num2 = str2double(tmp(2));
             if ~isnan(num2)
                 out = [out repmat(num2, 1, num1)];
             end;
         elseif size(tmp, 2) == 1 && ~isnan(num1)
             out(end + 1) = num1;
         end;
      end;
    end