1

I would like to read files containing numbers in each line. Here is the example of the file format-

boundary-points-x: 0.00 5.00
boundary-points-y: 0.00 0.10 0.20 0.30 4.90
boundary-points-z: 0.00 1.00 2.00 3.00 4.00 5.00

The number after literal text is the elements I want to read. Each line contains different number of elements, and the number of elements in each line may change from file to file. Thus, it is not possible to use textscan to read in same format. Is there any way to solve it? Thanks!

dinex
  • 367
  • 5
  • 15

1 Answers1

0

use regexp to parse the line:

fid = fopen('/path/to/file.txt', 'r');
line = fgetl(fid);
while ischar(line)
    res = regexp(line, '\s+(\d*\.?\d*)', 'tokens');
    literals = cellfun(@str2double, res);  % you have this line's literals. You decide what to do with them...
    line = fgetl(fid);
end
fclse(fid);

You can see here an explanation on the regular expression and an example how it works for parsing your line. You can also play around with regular expressions there.

Shai
  • 111,146
  • 38
  • 238
  • 371