error in matlab arithmetic operations ?

조회 수: 5 (최근 30일)
LO
LO 2020년 7월 7일
댓글: LO 2020년 7월 7일
I am puzzled by the result of this operation in matlab:
I have a var that equals 3
I am trying to calculate (var-1)*60
instead of getting 120 I get 3000, it may have something to do with number formatting ?
  댓글 수: 3
John D'Errico
John D'Errico 2020년 7월 7일
It should have nothing to do with formatting a number. Far more likely, the variable you used does not contain the number 3, or perhaps the multiplier is not 60, IF you got 3000 as a result. But we cannot know, because you have not shown what you really have, only what you THINK you have.
Possibly, you have a variable in your workspace that is equal to 3. But inside a function, you defined a variable with the same name, that has some other value. probably on the order of 50. So inside your function, you get a product of 3000. The resulting problem may lie in not understanding function workspaces. Or it may be due to something else, that is difficult to guess. Unless you provide an example where this happens, we cannot easily help you. Of course, you can never show an example where it will really happen, because it is not true that 2*60 is ever roughly 3000.
As I said, I'd conjecture this is a problem with function workspaces. It might even be an isssue with global variables, where some variable got changed and you never knew it did.
LO
LO 2020년 7월 7일
Thanks John
the var is actually not called var (as rightfully StarStrider pointed out, this would be a function). The var is just the value in a text box which equals 2 - I am sure of this: I tried to display separately in a text box (I am using a GUI with app designer) and it is = 2. I have no idea of why

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

채택된 답변

Steven Lord
Steven Lord 2020년 7월 7일
Your variable named var (which is a bad idea, because var already has a meaning in MATLAB) is not 3. It is '3'.
xDouble = 3;
(xDouble-1)*60
xChar = '3';
(xChar-1)*60
The ASCII value of '3' is 51. When you subtract 1 from '3' the result is the double value 50 and 50*60 is 3000.
  댓글 수: 1
LO
LO 2020년 7월 7일
that is what I wanted to hear :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by