string not accepted by switch

  • Follow


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:













7/10/2012 6:19:08 PM


Reply: