how to call structure data into a GUI popup menu?
Mostrar comentarios más antiguos
I have a pushbutton programmed to pull in data (3X1 struct with 43 fields). I then want to display the data from all rows of one field in a popup menu. I am struggling to get this to work. Do I need to convert it to a string first? How? Then how do I call that back into the popup... set(hObject,'String',??)?
popupmenu1_CreateFcn(hObject, eventdata, handles)
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
% 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
set(hObject,'String',???)
Respuestas (1)
Image Analyst
el 31 de Mayo de 2017
Don't do anything with any CreateFcn() functions. Put the code to load the strings into the popup in your pushbutton callback that reads in your data. Let's say that str43 is the name of your structure with 43 fields in it. And let's say the field called "field1" is the field that contains the data. And let's say you want to get that field from the second structure in your 3x1 structure array. You can get it by doing
field1 = str43(2).field1;
Now, you need to convert the rows of field1 into strings. How to do that depends on what kind of variable field1 is. If field1 is a cell array of strings, then you can do
handles.popupmenu1.String = field1;
If it's a row or column vector of floating point numbers, then you can do
for row = 1 : size(field1)
popupStrings{k} = sprintf('%f', field1(k)); % Convert number to string.
end
% Send cell array to the popup comtrol:
handles.popupmenu1.String = popupStrings;
If field1 is some other kind of variable, then you're going to have to figure out how you want it to appear in your popup list and do something to make the cell array of strings.
5 comentarios
Walter Roberson
el 31 de Mayo de 2017
An abbreviation for
for row = 1 : size(field1)
popupStrings{k} = sprintf('%f', field1(k)); % Convert number to string.
end
is
popupStrings = cellstr( num2str(field1(:)) );
Andrew Paulsen
el 31 de Mayo de 2017
Image Analyst
el 31 de Mayo de 2017
You can change the popup entries anytime you want. You can prepopulate with a list of strings in GUIDE, then change them at anytime you want to or need to. Simply make up a new list of strings in a cell array, and send it to the popup control. You can also do that with a listbox if you want. The listbox takes up more real estate but gives you the option to select multiple entries. A popup takes up less real estate and you can select only one item. Use whichever one suits your needs and wants.
Walter Roberson
el 31 de Mayo de 2017
Side note: to get more than one result from a listbox, you need to set its Max parameter to greater than 1.
Andrew Paulsen
el 31 de Mayo de 2017
Categorías
Más información sobre App Building en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!