How do i write this script in anonymous function

7 ビュー (過去 30 日間)
Jaeyoung Lee
Jaeyoung Lee 2019 年 3 月 7 日
編集済み: Guillaume 2019 年 3 月 8 日
disp('This program convert Celsius to Fahrenheit');
val = input('Type 1 for Celcius to Farenheit and Type 2 for vice versa: ');
switch val
case 1
Celsius=input('Write a temperature in Celsius and you''ll have the result in Fahrenheit: ');
disp([ 'x = ' num2str(Celsius) ' Celcius and y = ' num2str(Celsius*1.8+32) ' Fahrenheit']);
case 2
Faren=input('Write a temperature in Farenheit and you''ll have the result in Celcius: ');
disp([ 'x = ' num2str(Faren) ' Fahrenheit and y = ' num2str((Faren-32)/1.8) ' Celcius ' ]);
end
  2 件のコメント
Alex Mcaulley
Alex Mcaulley 2019 年 3 月 8 日
What do you want exactly? I think you don't need an anonymous function for this simple script
Guillaume
Guillaume 2019 年 3 月 8 日
編集済み: Guillaume 2019 年 3 月 8 日
An explanation of this odd request is indeed needed. Why does it need to be an anonymous function? Why can't it be a normal function or a local function or a nested function?
Bearing in mind that strictly speaking, it's impossible to convert this script into an anonymous function since anonymous functions in matlab can only consist of a single non-branching, non-assigning statement.

サインインしてコメントする。

回答 (1 件)

Omer Yasin Birey
Omer Yasin Birey 2019 年 3 月 8 日
編集済み: Omer Yasin Birey 2019 年 3 月 8 日
Hi Jaeyoung, as Alex said you probably don't need anonymous function for this script and it won't make the code much shorter. But you can use this one below.
disp('Type 1 for Celcius to Fahrenheit, Type 2 for vice versa');
x = inputdlg({'Celcius or Fahrenheit','Temperature'},...
'Converter',[1 60; 1 35]);
converter = {@(celcius) num2str(celcius*1.8+32);
@(fahren) num2str((fahren-32)/1.8);
};
converter{str2num(x{1})}(str2num(x{2}))

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by