Error using sscanf Invalid format.

9 ビュー (過去 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 Center および File ExchangeApp Building についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by