matlab importdata produces strange output if there are blank cells in a txt file

63 Views Asked by At

I want to import thef following txt file in matlab.

    Run       Local       Local      Local    Local   First-order
   Index     exitflag      f(x)     # iter   F-count   optimality
       1       -10                               14
       2         3    1.239e-09        39        40     3.484e-06
       3         3    3.028e-22        45        46     2.174e-12
       4       -10                                4
       5       -10                                7
       6         3    5.749e-13        17        18     5.438e-08
       7       -10                                3
       8       -10                                2
       9       -10                                5
      10       -10                                1
      11       -10                                2
      12       -10                                3
      13       -10                                5
      14       -10                                1
      15       -10                                2
      16       -10                                2

MultiStart completed some of the runs from the start points.

3 out of 16 local solver runs converged with a positive local solver exit flag.

As you can see, there are a couple of blank cells and lines of text at the end and beginning of the file. I am only interested in the numerical data, which is a 16x6 matrix for that particular txt file. The empty cells be NaN.

Importdata is able to separate the text from the numerical data, however, thinks that there are only 3 columns because there are three blank cells in the first row. If I remove the text lines manually and call readtable(), there are, for whatever reasons, the first three lines of the numerical data missing.

How can I get the 16x6 matrix from the text file where blank cells = NaN?

1

There are 1 best solutions below

0
matlaber2021 On

I will consider using regular expression instead of some functions like readtable(). But the following codes are not general and for only suggestions.

fid = fopen('result.txt');
data = '';
while ~feof(fid)
  tline = fgets(fid);
  data = [data, tline];
end
fclose(fid);

pattern1 = '\d.+?\r';
match = regexp(data,pattern1,'match');
match = match(:);

result = cell(numel(match), 6);
for k = 1 : numel(match)
  target = match{k};
  pat1 = '(\d{1,2})\s+(\S+?)\s+(\S+?)\s+(\S+?)\s+(\S+?)\s+(\S+)';
  tokens = regexp(target,pat1,'tokens');
  if ~isempty(tokens)
    result(k,:) = vertcat(tokens{:});
  else
    pat1 = '(\d{1,2})\s+(\S+?)\s+(\S+)';
    tokens = regexp(target,pat1,'tokens');
    result(k,[1,2,5]) = vertcat(tokens{:});
  end
end

result(cellfun(@isempty, result)) = {''};
result = str2double(result);

disp(result)