Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
User defined length of pop-up menu GUI

Subject: User defined length of pop-up menu GUI

From: Clay Ross

Date: 20 Aug, 2009 18:16:18

Message: 1 of 5

Hello.

If I have an array of 1:n, how can I construct a pop-up menu with selections of 1:n? The user will be responsible for determining the value of "n." I just learned how to program GUI's in MATLAB today, so hopefully this isn't hard to do.

Thanks!

Clay

Subject: User defined length of pop-up menu GUI

From: Andy

Date: 20 Aug, 2009 18:41:18

Message: 2 of 5

"Clay Ross" <clayton.e.ross@gmail.com> wrote in message <h6k3ti$1a1$1@fred.mathworks.com>...
> Hello.
>
> If I have an array of 1:n, how can I construct a pop-up menu with selections of 1:n? The user will be responsible for determining the value of "n." I just learned how to program GUI's in MATLAB today, so hopefully this isn't hard to do.
>
> Thanks!
>
> Clay

% here is an example

function testgui

f=figure;

edit1=uicontrol('Parent',f,...
    'Style','edit',...
    'Units','normalized',...
    'Position',[.2 .6 .2 .1],...
    'Callback',{@edit1_Callback});

pop1=uicontrol('Parent',f,...
    'Style','popupmenu',...
    'Units','normalized',...
    'Position',[.2 .2 .2 .1],...
    'String','Enter a number');

    function edit1_Callback(src,eventdata)
        n=get(edit1,'String');
        n=str2double(n);
        set(pop1,'String',num2cell(1:n)');
    end

end

% Also see the following for lots of answers to common questions about GUIs:
% http://www.mathworks.com/matlabcentral/fileexchange/24861

Subject: User defined length of pop-up menu GUI

From: Nathan

Date: 20 Aug, 2009 18:42:28

Message: 3 of 5

On Aug 20, 11:16 am, "Clay Ross" <clayton.e.r...@gmail.com> wrote:
> Hello.
>
> If I have an array of 1:n, how can I construct a pop-up menu with selections of 1:n? The user will be responsible for determining the value of "n." I just learned how to program GUI's in MATLAB today, so hopefully this isn't hard to do.
>
> Thanks!
>
> Clay

Put something like this in your edit-box callback (where the user
chooses n)
tmp = str2double(get(handles.edit,'string'));
set(handles.popupmenu,'string',1:tmp')


-Nathan

Subject: User defined length of pop-up menu GUI

From: Clay Ross

Date: 20 Aug, 2009 18:56:19

Message: 4 of 5

"Andy " <theorigamist@gmail.com> wrote in message <h6k5ce$688$1@fred.mathworks.com>...
> "Clay Ross" <clayton.e.ross@gmail.com> wrote in message <h6k3ti$1a1$1@fred.mathworks.com>...
> > Hello.
> >
> > If I have an array of 1:n, how can I construct a pop-up menu with selections of 1:n? The user will be responsible for determining the value of "n." I just learned how to program GUI's in MATLAB today, so hopefully this isn't hard to do.
> >
> > Thanks!
> >
> > Clay
>
> % here is an example

That is perfect! Thanks a lot Andy!
>
> function testgui
>
> f=figure;
>
> edit1=uicontrol('Parent',f,...
> 'Style','edit',...
> 'Units','normalized',...
> 'Position',[.2 .6 .2 .1],...
> 'Callback',{@edit1_Callback});
>
> pop1=uicontrol('Parent',f,...
> 'Style','popupmenu',...
> 'Units','normalized',...
> 'Position',[.2 .2 .2 .1],...
> 'String','Enter a number');
>
> function edit1_Callback(src,eventdata)
> n=get(edit1,'String');
> n=str2double(n);
> set(pop1,'String',num2cell(1:n)');
> end
>
> end
>
> % Also see the following for lots of answers to common questions about GUIs:
> % http://www.mathworks.com/matlabcentral/fileexchange/24861

Subject: User defined length of pop-up menu GUI

From: Yair Altman

Date: 21 Aug, 2009 08:28:02

Message: 5 of 5

On a related note, by default Matlab's popup menus only display M=20 items at a time. If the popup string has N>M items, the uicontol uses a scrollbar to display a moving window of M items. Here is how to modify this default value of the window-size M:

   % Setup a demo popup-menu uicontrol
   n=1:50; s=arrayfun(@num2str,n,'un',0);
   hPopup = uicontrol('style','popupmenu','string',s);

   % Modify the uicontrol's window size
   jPopup = findjobj(hPopup);
   set(jPopup,'MaximumRowCount',10); % or any other value

For this to work, you will need to first download the findjobj utility from the Matlab File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/14317

Yair Altman
http://UndocumentedMatlab.com
 

Tags for this Thread

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.

Contact us