How to run a Absolute value for formula when there's a variable?
5 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
dpb
on 15 May 2020
Edited: dpb
on 16 May 2020
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...
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!