Skipping certain lines using fscanf

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 Commenti

Attach data (not the screen shot).

Accedi per commentare.

Risposte (3)

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"
S =
"-4 -7.6 Point 1 4 -36.4 Point 5 1 xmin 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)
lead_number = 1×4
-4 4 1 3
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)
following_text = 1×4 cell array
{' -7.6 Point 1'} {' -36.4 Point 5'} {' xmin'} {' xmax'}
second_number = cellfun(@(str) sscanf(str, '%f%*[^\n]'), following_text, 'uniform', 0)
second_number = 1×4 cell array
{[-7.6000]} {[-36.4000]} {0×0 double} {0×0 double}
numbers = cellfun(@(n1, n2) [n1, n2], num2cell(lead_number), second_number, 'uniform', 0)
numbers = 1×4 cell array
{[-4 -7.6000]} {[4 -36.4000]} {[1]} {[3]}
So it's done... but it isn't pretty. There are better ways.

2 Commenti

Yes it is quite heavy- what is better alternative for it?
S = "-4 -7.6 Point 1" + newline + "4 -36.4 Point 5" + newline + "1 xmin" + newline + "3 xmax"
S =
"-4 -7.6 Point 1 4 -36.4 Point 5 1 xmin 3 xmax"
numbers = cellfun(@(str) sscanf(str, '%f', [1 inf]), regexp(S, '^[0-9\.eE\-\+ \t]+', 'match', 'lineanchors'), 'uniform', 0)
numbers = 1×4 cell array
{[-4 -7.6000]} {[4 -36.4000]} {[1]} {[3]}

Accedi per commentare.

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
Stephen23
Stephen23 il 20 Set 2022
Modificato: Stephen23 il 20 Set 2022
C = {};
[fid,msg] = fopen('polydata.txt','rt');
assert(fid>=3,'%s',msg)
while ~feof(fid)
C{end+1} = sscanf(fgetl(fid),'%f',[1,Inf]);
end
fclose(fid);
Imported data:
C{:}
ans = 1×2
-4.0000 -7.6000
ans = 1×2
-2.0000 -17.2000
ans = 1×2
0.2000 9.2000
ans = 1×2
1.0000 -1.6000
ans = 1×2
4.0000 -36.4000
ans = 1
ans = 3
ans = 0.2000
ans = []

Prodotti

Release

R2022b

Richiesto:

il 20 Set 2022

Modificato:

il 20 Set 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by