Switch case with cell gives an error

2 views (last 30 days)
I have this switch case but gives me an error because str is a cell. What can I do to correct it? The str is a cell 1x2.
str(1)='Yes'
str(2)='No'
str=get(g.rd,'string');
switch str
case 'Yes'
state='yes';
case 'No'
state='no';
end

Answers (2)

Walter Roberson
Walter Roberson on 31 May 2013
str(1) = {'Yes'}
or
str{1} = 'Yes';
Your code would have failed on the first assignment.
When you get() the 'string' property from g.rd, is the result a character vector (string) or a character array (such as can occur if you have multiple lines in the string property) or is it going to be a cell array of strings?
Are you certain that the 'string' property will contain exactly one row?
Is there a reason you are not using
state = lower(str) %assuming str is a character vector

Giorgos Papakonstantinou
Giorgos Papakonstantinou on 31 May 2013
I am using the debugger and my code fails at switch. And sorry I didn't mention it. It is a cell array of strings.
I correct My first post it is 2x1. So
str =
'Yes'
'No'
About the last one you 're right. I could have done it like this but this doesn't affect anything.
  2 Comments
Giorgos Papakonstantinou
Giorgos Papakonstantinou on 31 May 2013
I just wanted to implement a simple gui with two push buttons. 'Yes' and 'No'. I just replaced the switch/case because I don't understand what it it needs to get to work with if/else. Below I have my gui. I would appreciate if tell me what it needs to get to work with switch/case. But any way thank you Walter!
function [] = hide(currfig)
g.fh = figure('units','pixels',...
'position',[300 300 350 100],...
'menubar','none',...
'Color','w',...
'name','none',...
'numbertitle','off',...
'resize','off');
g.bg = uibuttongroup('units','pix',...
'pos',[0 0 350 50],...
'ForegroundColor','w');
g.rd(1) = uicontrol(g.bg,...
'style','push',...
'unit','pix',...
'position',[180 15 70 22],...
'Fontsize',8,...
'string','Yes');
g.rd(2) = uicontrol(g.bg,...
'style','push',...
'unit','pix',...
'position',[260 15 70 22],...
'Fontsize',8,...
'string','No');
set(g.rd,'callback',{@hide,g});
function hide(varargin)
g = varargin{3};
str=[get(g.rd(1),'val'),get(g.rd(2),'val')];
if str(1)==1
st='yes';
else
st='no';
end
assignin('base','st',st)
close(g.fh)
Walter Roberson
Walter Roberson on 31 May 2013
Is there any point in getting the val of g.rd(1) and g.rd(2) but then only making use of the first value?
If the result of get(g.rd,'string') is a cell array of strings, {'Yes', 'No'} then getting just the string does not tell you anything about the state.
Accessing the Value rather than the String is the right thing to do. The "if" you have just above is fine.
Have you considered using a toggle button or radio button, instead of two push buttons in a uibuttongroup ?
But if you really want to see an example of switching on a string, then:
function hide(varargin)
g = varargin{3};
str1 = cellstr( get(g.rd(1), 'string') );
str2 = cellstr( get(g.rd(2), 'string') );
str2 = {str1{1}; str2{1}};
%choice should come out 1 if the first button was selected, 2 if the second button was selected
choice = get(g.rd(1), 'Value') + 2 * get(g.rd(2), 'Value');
str = str12{choice};
switch str
case 'Yes'
state='yes';
case 'No'
state='no';
end
assignin('base', 'st', state);
The above is not how I would bother to code it, considering it can be done like
function hide(varargin)
g = varargin{3};
st = get(g.rd(1), 'Value');
str12 = {'yes'; 'no'};
state = str12{2 - st};
assignin('base', 'st', state);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!