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!