how to call structure data into a GUI popup menu?

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
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

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
Andrew Paulsen el 31 de Mayo de 2017
Walter,
How would you then dynamically create variables or separate the values to populate the popup? From my basic understanding of GUIDE and popups is that I need to populate with strings, record what string or value was selected to then perform the next operation. I have the structure field list now as a 3x1 cell. Can I populate the popup with this cell and easily differentiate which one was chosen? Would a listbox make more sense where multiple can be selected? Is there any benefit of using Application Developer or is that still the same concept, just a refresh of some graphics?
Thanks.
Image Analyst
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.
Side note: to get more than one result from a listbox, you need to set its Max parameter to greater than 1.
Andrew Paulsen
Andrew Paulsen el 31 de Mayo de 2017
Its finally starting to click. Thanks IA and Walter for all the quick responses. Just trying to push through and learn by trial and error it took me a bit to save and recall data from handle to handle. Once I got that it makes sense. My last hurdle is how to display results in a static textbox. I used a post by IA, though not getting it to work.

Iniciar sesión para comentar.

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 31 de Mayo de 2017

Comentada:

el 31 de Mayo de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by