0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rachel
  • 305
  • 2
  • 12

2 Answers2

1

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
prgao
  • 1,767
  • 14
  • 16
  • it shouldn't stop reading because '?' is a valid string, i.e. %s. I edited in a code example. – prgao Sep 29 '13 at 06:25
  • have you tried the code? it does read question mark as `nan`. that's what the `str2double` part is doing. why don't you try it in matlab by typing `str2double('?')` – prgao Sep 29 '13 at 06:29
0

You can use TEXTSCAN and specify 'TreatAsEmpty' as '?'.

Aaditya Kalsi
  • 1,039
  • 8
  • 16