There must be something I still don't understand about strings in
matlab, or how switch works.
It seems like the following code should work, in fact, it is directly
from the matlab demo of using GUIDE for creating graphical interfaces
(with the addition of the str(val) line). Here is the code, and the
output of executing this below.
Thanks for any ideas on why this switch does not evaluate str(val) to
'menbrane' and go to the code for that case. Thanks much....
------------
val=get(hObject,'Value')
str=get(hObject,'String')
str(val)
switch str(val)
case 'peaks'
handles.current_data=handles.peaks;
case 'membrane'
handles.current_data=handles.membrane;
case 'sinc'
handles.current_data=handles.sinc;
end
----------
val =
2
str =
'peaks'
'membrane'
'sinc'
ans =
'membrane'
??? SWITCH expression must be a scalar or string constant.
Error in ==> firstGUI>popupmenu1_Callback at 135
switch str(val)
Error in ==> gui_mainfcn at 75
feval(varargin{:});
Error in ==> firstGUI at 42
gui_mainfcn(gui_State, varargin{:});
??? Error while evaluating uicontrol Callback.
>>
------------
|
|
0
|
|
|
|
Reply
|
billjosephson (77)
|
1/2/2007 9:52:16 AM |
|
BillJosephson wrote:
> There must be something I still don't understand about strings in
> matlab, or how switch works.
>
> It seems like the following code should work, in fact, it is directly
> from the matlab demo of using GUIDE for creating graphical interfaces
> (with the addition of the str(val) line). Here is the code, and the
> output of executing this below.
> Thanks for any ideas on why this switch does not evaluate str(val) to
> 'menbrane' and go to the code for that case. Thanks much....
>
> ------------
> val=get(hObject,'Value')
> str=get(hObject,'String')
> str(val)
> switch str(val)
> case 'peaks'
> handles.current_data=handles.peaks;
> case 'membrane'
> handles.current_data=handles.membrane;
> case 'sinc'
> handles.current_data=handles.sinc;
> end
> ----------
> val =
> 2
>
> str =
> 'peaks'
> 'membrane'
> 'sinc'
>
> ans =
> 'membrane'
>
> ??? SWITCH expression must be a scalar or string constant.
>
> Error in ==> firstGUI>popupmenu1_Callback at 135
> switch str(val)
>
> Error in ==> gui_mainfcn at 75
> feval(varargin{:});
>
> Error in ==> firstGUI at 42
> gui_mainfcn(gui_State, varargin{:});
>
> ??? Error while evaluating uicontrol Callback.
>
> >>
> ------------
One solution is to use strcmp and if instead:
if strcmp(str(val),'peaks')
do this
elseif strcmp(str(val),'membrane')
do that
elseif strcmp(str(val),'sinc')
do the other
else
error('No corresponding string')
end
|
|
0
|
|
|
|
Reply
|
ArthurDoodson (14)
|
1/2/2007 10:01:11 AM
|
|
BillJosephson wrote:
>SNIP ... cell array of strings indexing problem
Since str is a cell array, so is str(val).
Use curly braces, to get the contents of the cell:
switch str{val}
hth
Jos
|
|
0
|
|
|
|
Reply
|
Jos
|
1/2/2007 10:06:52 AM
|
|
Jos wrote:
> BillJosephson wrote:
> >SNIP ... cell array of strings indexing problem
>
> Since str is a cell array, so is str(val).
> Use curly braces, to get the contents of the cell:
> switch str{val}
>
> hth
> Jos
And indeed, the code from the demo used curly braces. Need to get a
bigger monitor!
This fixed the problem, thanks a lot.
|
|
0
|
|
|
|
Reply
|
billjosephson (77)
|
1/2/2007 3:59:07 PM
|
|
|
3 Replies
27 Views
(page loaded in 2.148 seconds)
Similiar Articles: SWITCH expression must be a scalar or string constant - comp.soft ...Matlab: string not accepted by switch - programming.itags.org SWITCH expression must be a scalar or string constant. > Error in ==> firstGUI>popupmenu1_Callback at 135 ... initializer must be constant error bug? - comp.compilers.lcc ...... to support C99, maybe not > implemented yet?, or a switch I'm missing? Oh, if that wasn't accepted ... SWITCH expression must be a scalar or string constant - comp.soft ... Something bad happened to my gfortran installation - comp.lang ...... 0))) string because when I switch over to: character(16) string ... initialization expression it is accepted ... local character variables, whose string length is not ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... sshd closes connection immediately after login - comp.security.ssh ...I have not fiddled with the sshd configuration files ... mode for protocol 2.0 debug1: Local version string SSH-2 ... I just tried starting sshd with the -r switch and the ... Table with 400000 rows takes a long time to count - comp.databases ...... for your assistance - Paul's original recommendation to switch ... What was the philosophy > behind this, and why is this accepted as the norm - should the > database not be ... Converting from Y-m-d h:m:s - comp.protocols.time.ntp... tm); This would work even on systems that do not use ... of each 50 or 60 Hz cycle, then you phaselock your switch ... [tempo] Convert variable from string to number - comp.emacs ... NMEA ref.clock better than my ISP's timeserver? - comp.protocols ...You have to makes sure you do not degrade > it. Accepted, the ... ITX ME6000 600MHz C3 NetBSD-5.0 NMEA string ... ntpd before without problem and can always switch ... How to check whether malloc has allocated memory properly in case ...>That always struck me as a misleading switch name because that was *NOT ... device, or operations on character strings ... Since his thesis had already been accepted and ... Where did Fortran go? - comp.lang.fortranMATLAB yes, C not that much (judging from the number ... With F2003 at hand, this switch of languages F->C looks ... there was no commitment to play according to accepted ... Matlab: string not accepted by switch - programming.itags.orgprogramming.itags.org: Matlab question: string not accepted by switch, created at:Sun, 18 May 2008 20:33:00 GMT with 1,302 bytes, last updated: Thursday, July 05 ... Switch Statement with Strings in Java - Stack Overflowup vote 189 down vote accepted ... In de-sugaring a switch on String objects, both instructions are likely to be used. 7/10/2012 6:19:08 PM
|