error with sprintf add operation between 2 terms resulting in string

조회 수: 1 (최근 30일)
I used sprintf Mt1a to receive a value which someone not experienced with matlab would understand the value ,there appears to be a conflict in " Mt1a+Mt2a " which gives me multiple values ,i tried using str2double to fix my result but it returns NaN ,how can i fix the result for b ?
b=sprintf('%.0f\n',a./str2double(Mt1a+Mt2a)) % ==>how i attempted to use str2double
Mt2a=66539;
Fa=88050;
d2p=37;
p=6;
rb=0.08;
tga2p=p/(pi*d2p);
x=(tga2p+rb)/(1-(tga2p*rb));
Mt1a=sprintf('%.0f\n',Fa*(d2p/2)*x)
a=Fa*(d2p/2)*tga2p;
b=a./(Mt1a+Mt2a)
  댓글 수: 2
Stephen23
Stephen23 2021년 3월 25일
편집: Stephen23 2021년 3월 25일
Mt1a=sprintf('%.0f\n',Fa*(d2p/2)*x) % <- why are you converting this to character?
Mt1a+Mt2a % <- did you look at the output when you "add" two character vectors?
Mixing character vectors into your numeric operations is hindering you, not helping you. Get rid of them.
Opariuc Andrei
Opariuc Andrei 2021년 3월 25일
편집: Opariuc Andrei 2021년 3월 25일
if i remove sprintf from Mt1a the value for Mt1a+Mt2a is fixed but the value for Mt1a without sprintf =>Mt1a =
2.152845579898380e+05 . the value with sprintf =>Mt1a=215285 . I used sprintf because that's the only command i know of ,to get from Mt1a= 2.152845579898380e+05 toMt1a=215285.
Mt1a(with sprintf)+Mt2a= ss.

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

채택된 답변

Star Strider
Star Strider 2021년 3월 25일
I used sprintf because that's the only command i know of ,to get from Mt1a= 2.152845579898380e+05 toMt1a=215285.’
They are the same. Use the format function to change the way they appear in the workspace.
If you want to eliminate the fractional part of the numbers, use the round, fix, floor or ceil functions, depending on the result you want. (I provided a link to round, links to the others are in that documentation.)
  댓글 수: 3

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

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 3월 25일
Do you want to concatenate a number and a piece of text together?
a = '1234'; % a is a char vector
b = 5;
c = [a, num2str(b)]
c = '12345'
as = "1234"; % as is a scalar string
b = 5;
cs = as + b % automatically converts 5 to "5" then appends it to as
cs = "12345"
Or do you want to add them?
d = a + b % add 5 to the ASCII values of the characters '1', '2', '3', and '4'
d = 1×4
54 55 56 57
d2 = double('6789')
d2 = 1×4
54 55 56 57
f = str2double(a)+b % add 5 to the number represented by a
f = 1239
f2 = double(as)+b % double on a string array converts it to the number it represents
f2 = 1239
  댓글 수: 1
Opariuc Andrei
Opariuc Andrei 2021년 3월 25일
this is a homework of sorts , i've got this late 50's / 60 ish year old teacher that doesn't know how to use a pc , he learned how to present our study material in google meet last year because of online classes and that's about all he knows , even though i'm a newbie :) in terms of matlab ,i wanted to do my HW in matlab cause it's easier for me to work , and present my HW in a simple to ridiculous way so that anyone who hasn't seen/worked in matlab before would understand . and thus i needed easy to read(as in reading from paper ) results

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by