How to make textscan robust against non-matching lines?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joan Vazquez
el 8 de Abr. de 2021
Comentada: Stephen23
el 9 de Abr. de 2021
I have files with lines that I want to parse, preferably with textscan. In between those lines, there may be lines to be skipped (unpredictable format and abundance, but definetely new lines). What is the best way to deal with it?
E.g. for the data in attachment, this will stop outputiing #HELLOMATHWORKS messages after line 4.
fid = fopen('data.txt');
out = textscan(fid,'#HELLOMATHWORKS,%[^,],%n');
fclose(fid);
This is a MWE out of a large code base.
0 comentarios
Respuesta aceptada
Stephen23
el 8 de Abr. de 2021
Editada: Stephen23
el 9 de Abr. de 2021
str = fileread('data.txt');
tkn = regexp(str,'#HELLOMATHWORKS,([^,]+),(\S+)','tokens');
tkn = vertcat(tkn{:})
vec = str2double(tkn(:,2))
2 comentarios
Stephen23
el 9 de Abr. de 2021
@Joan Vazquez: I presume that the text #HELLOMATHWORKS is not what is actually in your file. If the actual text contains some unique character that does not exist anywhere else in the file, you might be able to leverage the LineEnding/EndOfLine option to achieve the goal of reading the file data using textscan.
Más respuestas (1)
Ver también
Categorías
Más información sobre Text Data Preparation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!