Error using sscanf Invalid format.

조회 수: 14 (최근 30일)
Jordan Stocker
Jordan Stocker 2020년 6월 5일
편집: Stephen23 2020년 6월 5일
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
Stephen23
Stephen23 2020년 6월 5일
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 2020년 6월 5일
didnt know about that thanks for telling me that did work by the way

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 6월 5일
편집: Stephen23 2020년 6월 5일
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
Jordan Stocker
Jordan Stocker 2020년 6월 5일
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 2020년 6월 5일
편집: Stephen23 2020년 6월 5일
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
>>

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by