Using sscanf and NaN

조회 수: 6 (최근 30일)
Sebastian Ciuban
Sebastian Ciuban 2014년 6월 6일
댓글: Geoff Hayes 2014년 6월 9일
Greetings,
So if i have the following:
a = '21414839.260 112535757.19909 2294.184 21414838.320 87690207.10148 1787.672'
Using:
A=sscanf(a,'%f')
Will return:
A =
1.0e+08 *
0.2141
1.1254
0.0000
0.2141
0.8769
0.0000
But if If the last 3 numbers are missing from the line :
a = '21414839.260 112535757.19909 2294.184 '
I want this to be returned:
A =
1.0e+08 *
0.2141
1.1254
0.0000
NaN
NaN
NaN
How can I make this possible?

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 6월 6일
If you know that sscanf of the input string is to return 6 numbers, and A is only populated with three, then you could do the following
A = [A; repmat(NaN,6-length(A),1)];
  댓글 수: 16
Sebastian Ciuban
Sebastian Ciuban 2014년 6월 9일
I don't know how to thank you for your patience and help!
Geoff Hayes
Geoff Hayes 2014년 6월 9일
It was fun!

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

추가 답변 (1개)

Star Strider
Star Strider 2014년 6월 6일
Use sscanf with two output arguments:
% Known input length
L = 6;
a = '21414839.260 112535757.19909 2294.184'
[A, knt] = sscanf(a, '%f');
A(knt+1:L) = NaN;
produces:
A =
21.4148e+006
112.5358e+006
2.2942e+003
NaN
NaN
NaN
  댓글 수: 2
Sebastian Ciuban
Sebastian Ciuban 2014년 6월 6일
Thank you for your answer!
Star Strider
Star Strider 2014년 6월 6일
My pleasure!

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

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by