MATLAB Answers

How to run a Absolute value for formula when there's a variable?

4 views (last 30 days)
Ibrahim AlZoubi
Ibrahim AlZoubi on 15 May 2020
Edited: dpb on 16 May 2020
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

Sign in to comment.

Accepted Answer

dpb
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)


Translated by