How to run a Absolute value for formula when there's a variable?
3 次查看(过去 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!