matlabtextscan

Replace a line containing a special character with nan using textscan in matlab


I am trying to read a file using textscan containing following lines

1.0 2.0 3.0 ? 6.0 5.0

11.0 12.0 13.0 15.0 16.0 15.0

21.0 22.0 23.0 25.0 26.0 25.0

31.0 32.0 33.0 ? 36.0 35.0

How can I replace the question marks with nan?


Solution

  • have you tried doing textscan with %s %s %s %s %s %s and using str2double to convert the resulting cell array to numbers

    f = fopen('file.txt');
    raw = textscan(f, '%s %s %s %s %s %s');
    data = [];
    for k = 1:6
        data = [data str2double(raw{k})];
    end