Thread Subject: using case statement on string inputs when number of case choices is unknown

Subject: using case statement on string inputs when number of case choices is unknown

From: aashay vanarase

Date: 18 Mar, 2010 18:55:20

Message: 1 of 6

i hav designed a drop down list in my gui that contains list of files present in a folder... but these files are diff each time the folder changes...

so how do i get the handles for strings that change.. i cant set the 'value' as i dont kno no. of files in the folder... can 'case' be used while incrementing an array, where the array elements are conditionalities of the case statemnt

any help will be appreciated

Subject: using case statement on string inputs when number of case choices

From: Walter Roberson

Date: 18 Mar, 2010 19:20:15

Message: 2 of 6

aashay vanarase wrote:
> i hav designed a drop down list in my gui that contains list of files
> present in a folder... but these files are diff each time the folder
> changes...

> so how do i get the handles for strings that change.. i cant set the
> 'value' as i dont kno no. of files in the folder...

I'm not entirely sure what you are asking.

If you want to find out which file was selected, then get() the Value of the
uicontrol, and get() the String property of the uicontrol, and index the
String property by the value.

Note: when you are doing this, use iscell() to determine whether the String
property returned a cell array or a character array. If it returned a cell
array, then the file name is

TheVariableName{TheIndex}

However, if it returned a character array, then the file name is

deblank(TheVariableName(TheIndex,:))


 > can 'case' be used
 > while incrementing an array, where the array elements are
 > conditionalities of the case statemnt


Yes it can, but that is an advanced and obscure technique, and I very much
doubt that you really want to do that. Such code is almost always better off
written as an if/elseif/elseif/end flow, or as a while statement that contains
an if condition.

Subject: using case statement on string inputs when number of case choices

From: aashay vanarase

Date: 19 Mar, 2010 06:00:07

Message: 3 of 6


> If you want to find out which file was selected, then get() the Value of the
> uicontrol, and get() the String property of the uicontrol, and index the
> String property by the value.
>
 i cant seem to get the 'string' that is currently selected from the whole dropdown list... i want the selected string, not the whole list of all options of the string value

Subject: using case statement on string inputs when number of case choices

From: Husam Aldahiyat

Date: 19 Mar, 2010 06:28:02

Message: 4 of 6

"aashay vanarase" <aashayv@gmail.com> wrote in message <hnv3t7$m4j$1@fred.mathworks.com>...
>
> > If you want to find out which file was selected, then get() the Value of the
> > uicontrol, and get() the String property of the uicontrol, and index the
> > String property by the value.
> >
> i cant seem to get the 'string' that is currently selected from the whole dropdown list... i want the selected string, not the whole list of all options of the string value

use

a = get(handle,'string') % whole strings
b = get(handles,'value') % index of selected string

c = a{b}; % selected string

Subject: using case statement on string inputs when number of case choices

From: aashay vanarase

Date: 19 Mar, 2010 12:23:03

Message: 5 of 6

H1 = findobj(gcf,'Tag','popupmenu');
a = get(H1,'string') % whole strings
b = get(H1,'value') % index of selected string
c = a{b}; % selected string

where should i put this prog so that value is updated every time user selects diff item in drop down list.. i am facing this prob as my popup list is generated in the callback of a push button...

here's my prog...

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)

location=uigetdir;
imagefiles=dir(location);
imagenames= {'select faulty image'};
for index = 1:length(imagefiles)
singlefile= imagefiles(index).name;
[folder, name, extension, version] = fileparts(singlefile);
extension = lower(extension);
switch (extension)
case {'.jpg'}
imagenames = [imagenames; singlefile];
otherwise
end
end
h = uicontrol('Style','popupmenu', 'String', imagenames,'Position',[100 200 200 10],'callback','dispimg');
imagenames = get(h,'String')
iv=get(h,'value')

callback 'dispimg' is:

H1 = findobj(gcf,'Tag','popupmenu');
a = get(H1,'string') % whole strings
b = get(H1,'value') % index of selected string
c = a{b}; % selected string

here iv always displays '1' as it is set before user has a chance to select, but when i try to get value in the call back, i get empty sets...

when i change selected object it returns
a =

     []


val =

     []

Subject: using case statement on string inputs when number of case choices

From: aashay vanarase

Date: 19 Mar, 2010 12:24:04

Message: 6 of 6

H1 = findobj(gcf,'Tag','popupmenu');
a = get(H1,'string') % whole strings
b = get(H1,'value') % index of selected string
c = a{b}; % selected string

where should i put this prog so that value is updated every time user selects diff item in drop down list.. i am facing this prob as my popup list is generated in the callback of a push button...

here's my prog...

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)

location=uigetdir;
imagefiles=dir(location);
imagenames= {'select faulty image'};
for index = 1:length(imagefiles)
singlefile= imagefiles(index).name;
[folder, name, extension, version] = fileparts(singlefile);
extension = lower(extension);
switch (extension)
case {'.jpg'}
imagenames = [imagenames; singlefile];
otherwise
end
end
h = uicontrol('Style','popupmenu', 'String', imagenames,'Position',[100 200 200 10],'callback','dispimg');
imagenames = get(h,'String')
iv=get(h,'value')

callback 'dispimg' is:

H1 = findobj(gcf,'Tag','popupmenu');
a = get(H1,'string') % whole strings
b = get(H1,'value') % index of selected string
c = a{b}; % selected string

here iv always displays '1' as it is set before user has a chance to select, but when i try to get value in the call back, i get empty sets...

when i change selected object it returns
a =

     []


val =

     []

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com