Switch expression must be a scalar or string constant -- Matlab GUI

2 ビュー (過去 30 日間)
Giuseppe Napoli
Giuseppe Napoli 2017 年 7 月 20 日
コメント済み: Jan 2017 年 7 月 20 日
Hello to everyone,
I am creating a GUI where a user can select with radio button three different type of steels. For the first two type, I don't have a problem because I call "304" and "316". The third option instead is a string value called "STR18" and when I try to execute my script Matlab says " Switch expression must be a scalar or string constant". I think that the problem could be in the variable "steeltype", but I am not sure. How can I solve my problem? How can I use, at the same time, numbers and string for the switch function ?
Thanks in advance
function pushbutton1_Callback(hObject, eventdata, handles)
st= get(handles.bottoni,'SelectedObject');
steeltype=str2num(get(st,'String'));
riduzione= str2num(get(handles.riduzione,'String'));
e=riduzione;
switch steeltype
case 1
......
case 2
......
case 3
......
otherwise
end
  1 件のコメント
Stephen23
Stephen23 2017 年 7 月 20 日
Why convert your perfectly nice strings into numbers? Get rid of the str2num and use strings to specify the switch cases.

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

回答 (1 件)

Adam
Adam 2017 年 7 月 20 日
編集済み: Adam 2017 年 7 月 20 日
Just keep them all as strings. You can't convert STR18 to a number so don't convert the others either, just switch on the strings instead.
Incidentally you should use str2double rather than str2num.
  5 件のコメント
Giuseppe Napoli
Giuseppe Napoli 2017 年 7 月 20 日
Ok, I solved all my problem. Since I removed the str2num all the case have to put between the two apostrophe ('STR18').
Thanks all for your help. Have a nice evening.
Jan
Jan 2017 年 7 月 20 日
@Giuseppe: If the answer has helped you to solve the problem, please accept it.

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

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by