How to run a Absolute value for formula when there's a variable?
조회 수: 2 (최근 30일)
이전 댓글 표시
Why when I want to simulate this code: (considering value of Temperature is equal 1).
input Temperature
y=abs('Temperature')
The answer would be:
y =
84 101 109 112 101 114 97 116 117 114 101
instead of: 1
댓글 수: 0
채택된 답변
dpb
2020년 5월 15일
편집: dpb
2020년 5월 16일
Because
>> y=abs('Temperature');
>> char(y)
ans =
'Temperature'
>>
You passed the literal character string 'Temperature' to the abs() function, not a variable named Temperature
>> input Temperature
Temperature 1
ans =
1.00
>>
The above is bad syntax -- it runs, but you didn't use a variable name as the LHS in an assignment statement so the result of whatever the user would enter goes to the default built-in ans variable -- the string Temperature is the prompt displayed to the user, not a variable name that the result will go into.
You intended to write something like:
Temperature=input('Please enter a temperature value: ');
y=abs(Temperature);
NB: the variable Temperature is created and assigned a value by the input function; the string there is sufficiently explanatory to the user of what is expected and the argument to the abs function is the name of the variable not surrounded by quotes.
A sylistic point would be that y wouldn't seem a very meaningful name for that variable in lieu of something that reflects what it actually is, but that's not a fatal error, just one of making the code easier to follow for the humans involved...
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!