Question

Another GUIDE question.

I created a GUIDE figure with a pop up menu. The elements of the menu are determined dynamically when calling the figure. Example: myGUI(data, popupNames) should be called and then the strings included in popupNames should be used to label the choices. I am able to do that in the callback for the popup menu. However that means that the popup will not be populated until I actually press the on the popup menu and choose the first (curently empty) choice.

My question is how would I be able to populate it dynamically through the create function (or any other function). I also need to do the same thing with a uitable (populate the row and column names, though this one is not dynamic)

My failed attempt, which led me to realizing that I can't access the handle data in the create functions, was as follows:

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
handles.popupNames = varargin{2};
set(hObject, 'String', {handles.popupNames{1:end}});

% --- Executes during object creation, after setting all properties.
function uitable1_CreateFcn(hObject, eventdata, handles)

handles.data = varargin{1};

t = uitable(handles.uitable1);
set(t,'Data',handles.data{1})
set(t, 'ColumnName', {'a', 'b', 'c'})
set(t,'RowName', {'1', '2', '3'})
Était-ce utile?

La solution

GUIDE generates a myGui_OpeningFcn(hObject, eventdata, handles, varargin). In there, you can initialize your strings:

function myGui_OpeningFcn(hObject, eventdata, handles, varargin)

    handles.output = hObject;

    set(handles.popupmenu1, 'String', varargin{2});

    set(handles.uitable1, 'Data', varargin{1});
    set(handles.uitable1, 'ColumnName', {'a' 'b' 'c'});
    set(handles.uitable1, 'RowName', {'1' '2' '3'});

    guidata(hObject, handles);
end

Note the use of guidata which updates the handle structure for the GUI with the new handles. If you don't call guidata whenever you change the handles structure, the changes you've made won't be reflected.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top