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

조회 수: 1 (최근 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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by