MATLAB: How to populate a popup menu GUI from a structure list

guipopup menu

Struggling to get data into my popup menus to then select desired inputs to generate an output.
Here is my code… I have a push button that creates a MEX file and converts sie files to mat and generates a structure. Then I want three popup menus to select three channels to then solve. How do I populate my generated data1.ChannelName in the popup menus and then input those selections into variables?
% --- Executes on selection change in lat1.
function lat1_Callback(hObject, eventdata, handles)
% hObject handle to lat1 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)
items = get(hObject, 'String', data1.ChannelName);
index_selected = get(hObject, 'Value');
item_selected = items{index_selected};
display(item_selected);
% Hints: contents = cellstr(get(hObject,'String')) returns lat1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from lat1
% --- Executes during object creation, after setting all properties.
function lat1_CreateFcn(hObject, eventdata, handles)
% hObject handle to lat1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
set(hObject,'String',data1.ChannelName);
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

Best Answer

You can create the 3 popup menus with GUIDE. You can enter the strings you want them to display also in GUIDE, which is what I think you might mean by "get data into my popup menus". Double click the popup in GUIDE, which brings up the Property Inspector, and edit the String property to enter in the text you want it to display.
Then, in any callback function, you can get the item number that was selected by the user at run-time for each popup like this:
popupValue1 = handles.popup1.Value;
popupValue2 = handles.popup2.Value;
popupValue3 = handles.popup3.Value;
I don't know what "data1.ChannelName" is, or if there are 3 of them. Maybe you want to put the indexes of the popups into the ChannelName fields of 3 different structures. If so, you can do
data1.ChannelName = handles.popup1.Value;
data2.ChannelName = handles.popup2.Value;
data3.ChannelName = handles.popup3.Value;
popup1, popup2, and popup3 are the "tag" properties of the popups that you'll see when you double click the popup in GUIDE, which brings up the Property Inspector.
Related Question