Nazanin Manafi
Nazanin Manafi

Reputation: 23

Call Back Function for Popup menu in Matlab

I have generated a popup menu without using GUIDE by using following code which works well:

figure;
row=4;
String =sprintf('Video%d#', 1:row);
String(end) = [];
CString=regexp(String , '#' , 'split');
uicontrol('style','popupmenu' , ...
    'String' , CString ,...
    'Position' , [200,400,12,24]);

My problem is its callback function, I can not assign any function to that to have any action when I press any of it's option.

I will appreciate that anyone help me.

Upvotes: 1

Views: 2265

Answers (1)

wizclown
wizclown

Reputation: 313

I found this question because I was basically having the same problem. Even if this is (at the time of writing this answer) more than 1 year old, I am posting my solution hoping that it will help the posterity.

You can get the value of the property Value of your pop-up menu: that is basically the position, in the array of the possible choices that populate your pop-up menu, of the selected choice.

It is easier to code than to explain in words, so an example code follows. Just copy/paste this code into a plain text file with .m extension, and run it in Matlab.

function popupexample

    % create an empty figure
    h_fig = figure;

    % create a popup menu
    h_popup = uicontrol(...
        'Style','popupmenu',...
        'String',{'1st choice','2nd choice','3rd choiche','...and so on'},...
        'Callback',@mypopup_fcn,...
        'Units','normalized',...
        'Position',[0 0.5 1 0.5]);

    % create a textbox
    h_textbox = uicontrol(...
        'Style','edit',...
        'Units','normalized',...
        'Position',[0 0 1 0.5]);

    % the popup callback
    function mypopup_fcn(hObject,eventdata)
        my_selection = get(hObject,'Value');
        set(h_textbox,'String',my_selection)
    end

end

Upvotes: 1

Related Questions