Skipping certain lines using fscanf
59 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a text file as below and i want to read only the highlighted ones from the text.
I tried using below code, but the scanning stops as it encounters "xmin"- is there any way i can work around this and i should strictly use fscanf or fgetl functions only. Attached text file for reference.
A=fscanf(fid, '%f %f %*s %*f',[2,inf])';
2 comentarios
Respuestas (3)
Walter Roberson
el 20 de Sept. de 2022
Can it be done using fscanf(). Yes. But that doesn't mean that fscanf() is a good tool for the job.
S = "-4 -7.6 Point 1" + newline + "4 -36.4 Point 5" + newline + "1 xmin" + newline + "3 xmax"
values = sscanf(S, '%f%[^\n]%c', [1 inf]);
lengths = diff([0, find(values == newline), length(values)]);
split_values = mat2cell(values, 1, lengths);
lead_number = cellfun(@(V) V(1), split_values)
following_text = cellfun(@(V) char(V(2:end)), split_values, 'uniform', 0);
following_text = cellfun(@(str) str(~ismember(str, [10 13])), following_text, 'uniform', 0)
second_number = cellfun(@(str) sscanf(str, '%f%*[^\n]'), following_text, 'uniform', 0)
numbers = cellfun(@(n1, n2) [n1, n2], num2cell(lead_number), second_number, 'uniform', 0)
So it's done... but it isn't pretty. There are better ways.
2 comentarios
Walter Roberson
el 20 de Sept. de 2022
S = "-4 -7.6 Point 1" + newline + "4 -36.4 Point 5" + newline + "1 xmin" + newline + "3 xmax"
numbers = cellfun(@(str) sscanf(str, '%f', [1 inf]), regexp(S, '^[0-9\.eE\-\+ \t]+', 'match', 'lineanchors'), 'uniform', 0)
Bjorn Gustavsson
el 20 de Sept. de 2022
For this rather loose file-format specification that you've been given (meaning that in a couple of days, weeks or months it will be arbitrarily extended with lines of some other format that should be parsed somewhat differently. Your teacher might not have told you about this but this is the practical reality. If someone tells you to do this at an early stage of any project brnig a proper loggin ax to the next meeting, explain that some well-specified file-format should be chosen, if anyone argues against that hack the ax hard into the table, send my best whishes.)
For this I'd go about the task something like this:
0, in a loop (check for feof as per usual)
1, Read one line at a time as a string.
2, Put the parsing of the current string in a sequence of sscanf calls and check the error-message from its outputs
[A,COUNT,ERRMSG,NEXTINDEX] = sscanf(curr_line,'%f %f %*s %*f');
and so on for the formats of the other lines,
3, then save the requested results in whatever format desired after each match.
HTH
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings 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!