matlabmatlab-gui

Printing out variables in a pop up menu


the pop-up menu seems empty at first and after closing and reopening the menu the variables show up. how can I change it to show it in the first opening. this is the code :

% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns  popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2

% Identify the first popupm menu selected option
% not strictly necessary, just used to generare the messsage box      text
sel_op=get(handles.popupmenu1,'value');
% Idetify the selected option in the second popupmenu
opt=get(hObject,'value')
% Test the second popup menu selection:
    %if opt == 1: the default output file has been selected
if(opt == 1)
    %
    % Insert here the code to save the output in the default output file

goal look for pop up


Solution

  • Say you have your menu defined as Pop=uicontrol('style','popupmenu','string',' ');

    then anywhere in the code, where Pop is accessible you can use one of the lines:

    %// assign only 2 number to the Pop menu
    set(Pop, 'string', {num2str(val1); num2str(val2)});
    
    %// assign only 2 numbers with labels to the Pop menu     
    set(Pop, 'string', {...
            ['Val1 = ' num2str(val1)]; ...
            ['Val2 = ' num2str(val2)]});
    

    Or you can make it more readable and flexible by, for example:

    Pop_string = get(Pop, 'string');     %// Read actual String in Pop
    Pop_string{3} = ['Val3 = ', Val3];   %// Update 3rd element
    set(Pop, 'String', Pop_string);      %// Update the String in Pop
    

    Edit:

    I've made a sample code using anonymous functions, see details here: Access nested functions from GUI:

    function[]=activePop()
    close all,clc
    fig=figure;
    Pop=uicontrol('style','popupmenu','string',' ');
    uicontrol('style','pushbutton','string','Reset',...
      'callback',@(s,a)PushReset(),'position',[5 1 1 1].*get(Pop,'position'));
    uicontrol('style','pushbutton','string','Update',...
      'callback',@(s,a)PushUpdate(),'position',[10 1 1 1].*get(Pop,'position'));
    
      function PushReset()      %// Resets the Pop's list
        N=ceil(5*rand(1));    %// The menu will have 1 to 5 entries
        Labels=cell(N,1);
    
        for ii=1:N
            Labels{ii}=['Val' num2str(ii) ' = ' num2str(rand)];  %// assign 'Val(ii) = ' label and random value to the list
        end
    
        set(Pop,'string',Labels)   %// display the list in Pop's menu
      end
    
      function PushUpdate()   %// Change one (randomly selected) value in Pop's menu
        PopString=get(Pop,'string');   %// get actual List of entries
        N=size(PopString,1);           %// find it's size
        ii=ceil(N*rand);               %// pick one random element
        Line=PopString{ii};            %// read the chosen line
        Line=regexp(Line,' ','split'); %// extract the label
        Line=[Line{1},' ',Line{2},' ',num2str(rand)]; %// update the line
        PopString{ii}=Line;            %// update the line
        set(Pop,'string',PopString);   %// send updated list to the Pop menu
      end
    
    end