Error using sscanf Invalid format.

16 views (last 30 days)
this is my code and i am gettin the above mentioned error help would be apreciated
% Button pushed function: SelectFileButton
function SelectFileButtonPushed(app, event)
[F,P] = uigetfile();
Results1 = fullfile(P,F);
Results2 = fopen(Results1);
Results4 = [0,0,0];
FlowRatePulse = [];
Pressure = [];
n = 0;
Time = 0;
while n >= 0
n = n+1;
Results3 = fgetl(Results2);
Results5 = sscanf(Results3,Results4);
Pressure(n) =((-0.2+Results5(1,1))/0.0018)/5;
FlowRatePulse(n) = Results5(1,2)/330;
Time = Time + Results5(1,3);
end
plot(app.UIAxes,Pressure,FlowRatePulse,'o')
plot(app.UIAxes2,Time,Pressure,'o')
plot(app.UIAxes3,Time,FlowRatePulse,'o')
end
  2 Comments
Stephen23
Stephen23 on 5 Jun 2020
I note that you have already fixed your previous unrelated error using the answer that I gave you, but have not accepted the answer. On this forum it is considered polite to accept an answer when it resolves your original question.
Jordan Stocker
Jordan Stocker on 5 Jun 2020
didnt know about that thanks for telling me that did work by the way

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 5 Jun 2020
Edited: Stephen23 on 5 Jun 2020
The sscanf documentation explains its syntax as "A = sscanf(str,formatSpec) reads data from str, converts it according to the format specified by formatSpec..." and also specifies that " formatSpec can be a character vector in single quotes, or a string scalar."
Rather than following what the documentation requires, you have defined the format specifier as the numeric vector [0,0,0]. Instead of a numeric vector, you should be providing a character vector or a string scalar, as the documentation states. The documentation also has plenty of examples of the format specifier.
  2 Comments
Jordan Stocker
Jordan Stocker on 5 Jun 2020
the aim of the sscsnf is to remove the V counts and ms from the numbers so that the numbers may be used in calculations later
would this work
Results4 = (V:counts:ms);
or is there a better way to be doing this
Stephen23
Stephen23 on 5 Jun 2020
Edited: Stephen23 on 5 Jun 2020
One way would be to download my FEX submission sip2num and use that on the complete file string, e,g.:
>> str = fileread('Gradual opening of tap.txt');
>> mat = sip2num(str); % converts '500 m' to 0.5
>> mat = reshape(mat,3,[]).'
mat =
0.36168 0 0.5
0.35679 0 0.5
0.36168 0 0.5
0.36168 0 0.5
0.36657 0 0.5
0.36168 0 0.5
0.36168 0 0.5
0.36168 0 0.5
0.36168 0 0.5
0.36168 0 0.5
0.35679 0 0.5
0.35679 0 0.5
0.36168 0 0.5
0.35679 0 0.5
0.36168 0 0.5
0.35679 0 0.5
0.35679 0 0.5
0.34702 1 0.5
0.35191 3 0.5
0.35679 4 0.5
0.36168 3 0.5
0.35191 4 0.5
0.35191 4 0.5
0.35191 4 0.5
... lots more lines here
0.29814 35 0.5
0.30303 35 0.5
0.29326 35 0.5
0.30303 35 0.5
0.29814 34 0.5
0.29814 35 0.5
0.30303 35 0.5
0.30303 35 0.5
>>
If you really want to use fscanf or sscanf then you will need to define a suitable format string, e.g.:
>> mat = sscanf(str,'%f V,%f counts,%f ms',[3,Inf]).'
mat =
0.36168 0 500
0.35679 0 500
0.36168 0 500
0.36168 0 500
0.36657 0 500
0.36168 0 500
0.36168 0 500
0.36168 0 500
0.36168 0 500
0.36168 0 500
0.35679 0 500
0.35679 0 500
0.36168 0 500
0.35679 0 500
0.36168 0 500
0.35679 0 500
0.35679 0 500
0.34702 1 500
0.35191 3 500
0.35679 4 500
0.36168 3 500
0.35191 4 500
0.35191 4 500
... lots more lines here
0.30303 35 500
0.29326 35 500
0.30303 35 500
0.29814 34 500
0.29814 35 500
0.30303 35 500
0.30303 35 500
>>

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!