can I use switch case for strings?

hi i am trying to learn matlab and im really beginner at it.
can we use switch case for string ?
i really dont have a clue what am i doing here! i appriciate any help!
%This program is supposed to convert tempratur unite to other unit
c=input('kelvin,celsius,fahrenheit ');
switch c
case 'kelvin'
x=input('enter temprature')
C=x-273.5
disp('C')
R=1.8.*x
disp('R')
case 'celsius'
x=input('enter temprature')
K=x+273.5
disp('K')
F=1.8.*x+32
disp('F')
end

 採用された回答

Stephen23
Stephen23 2020 年 11 月 9 日

0 投票

c=input('kelvin,celsius,fahrenheit ','s');
% ^^^^ you need this option!

1 件のコメント

ela mti
ela mti 2020 年 11 月 9 日
thaks for your help!

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

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 11 月 9 日
編集済み: Cris LaPierre 2020 年 11 月 9 日

0 投票

Yes, you can. If you are expecting a string input, try adding 's' as a second input to the input function (the second syntax option). Otherwise, you must enter the text in quotes.
c=input('kelvin,celsius,fahrenheit ','s');
Result using your current code and using quotes
kelvin,celsius,fahrenheit "kelvin"
enter temprature80
x =
80
C =
-193.5000
C
R =
144
R
Result using the 's' option
kelvin,celsius,fahrenheit kelvin
enter temprature80
x =
80
C =
-193.5000
C
R =
144
R

1 件のコメント

ela mti
ela mti 2020 年 11 月 9 日
thanks a lot ! I got it

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

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2020 年 11 月 9 日

コメント済み:

2020 年 11 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by