Textscan doesn't do what it's told
Mostrar comentarios más antiguos
I have a function to read large data sets and extract the data but every once in a while there is a random string in there and it just stops so i told it to treat as empty but it didn't work and when it gave me the error and showed me the line the treat as empty part wasn't even shown.
This is the error
Error using textscan
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 14535, field number 46) ==> #IND00 13.038326 89.999989 53.000000 66.605282 359.999965
13.013328 90.000023 36.000000 210.258485 359.016463 15.170686 270.000018 48.000000 265.060631 180.778297 14.631801 270.000025 1.#QNAN0 1.#QNAN...
Error in importfile (line 13)
dataArray = textscan(fid, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'TextType', 'string',
'EmptyValue', NaN, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
Respuesta aceptada
Más respuestas (1)
Star Strider
el 13 de Jul. de 2017
I cannot follow your code. However, using frewind repositions the file pointer at the beginning of the file. I would use the fseek function instead.
This is from a previous Answer (that worked) and illustrates the sort of approach I would take with a file such as yours:
st = fseek(fidi, 1, 'bof'); % Position File After First Line
k1 = 1; % Counter
while (st == 0) && (~feof(fidi)) % Test For End Of File Or Unsuccessful ‘fseek’ File Position
data{k1} = textscan(fidi, '%f%f', 'Delimiter','\t', 'HeaderLines',4, 'CollectOutput',1);
st = fseek(fidi, 50, 'cof'); % Position File Pointer To Next Line After Stop
k1 = k1 + 1;
end
You will obviously have to experiment with it to get it to work with your file. I would retain the while conditions, since the combination will prevent an infinite loop if the file does not have a valid end-of-file indicator.
5 comentarios
michael warshowsky
el 13 de Jul. de 2017
Star Strider
el 13 de Jul. de 2017
I don’t have your file, so I can’t experiment with it.
michael warshowsky
el 13 de Jul. de 2017
Editada: michael warshowsky
el 13 de Jul. de 2017
Star Strider
el 13 de Jul. de 2017
One option might be to add to your textscan arguments:
'TreatAsEmpty',{'-1#IND00'}
and any other weird strings that exist in your file that you can find and define. Try that first.
If you still have problems, use your textscan format descriptor instead of mine, then see if my code works with your file.
michael warshowsky
el 13 de Jul. de 2017
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!