Populating popup menu based on push button? (GUI)

EDIT: This has been resolved with the follow-up from Walter Roberson.
Hello. I am having trouble adding values to a popup menu in a GUI. Here is my code:
function pushbutton1_Callback(hObject, eventdata, handles)
[code] % Code creates a nx1 vector of values from an Excel file and stores them in a variable called newz.
handles.z = newz % Adds newz to handles structure to be used later
function popupmenu1_Callback(hObject, eventdata, handles)
set(hObject,'String',{handles.z}), % Supposed to populate popup menu.
The popup menu is not supposed to be populated until the push button has been clicked.
How can I get my values in newz to be in the popup men?

 採用された回答

Adam
Adam 2017 年 8 月 7 日
編集済み: Adam 2017 年 8 月 7 日

0 投票

You are missing the
guidata( hObject, handles )
line from your pushbutton callback.
'handles' is just a structure. It goes out of scope like any other variable when your function ends so you have to give this instruction to tell the GUI to replace its version of the handles struct with your updated one.

4 件のコメント

Christina Paris
Christina Paris 2017 年 8 月 7 日
編集済み: Christina Paris 2017 年 8 月 7 日
That worked. Thank you!
Here's my new code:
function pushbutton1_Callback(hObject, eventdata, handles)
[more code]
newz = ... [defines nx1 vector]
guidata(hObject, newz)
function popupmenu1_Callback(hObject, eventdata, handles)
data = guidata(hObject)
set(hObject,'String',data),
Christina Paris
Christina Paris 2017 年 8 月 7 日
UPDATE: The solution is either very slow or will not work. It DID work once or twice, but now does not. I did not change anything. Any idea why that could be?
Here is my complete code:
function pushbutton1_Callback(hObject, eventdata, handles)
filename = uigetfile({'*.xlsx'}, 'File Selector');
[~, ~, raw] = xlsread(filename);
[r, c] = find(strcmp(raw, 'oz'));
z = cat(1, raw{r+1:end, c});
zcell = cat(1, raw(r+1:end, c));
[row, ~] = find(diff(z) ~= 0);
zlook = [zcell(row); zcell(end)];
newz(:, 1) = zlook(:, 1);
newz = cell2mat(newz);
guidata(hObject, newz)
function popupmenu1_Callback(hObject, eventdata, handles)
data = guidata(hObject)
set(hObject,'String',data),
Walter Roberson
Walter Roberson 2017 年 8 月 7 日
Your code
function popupmenu1_Callback(hObject, eventdata, handles)
data = guidata(hObject)
set(hObject,'String',data)
says that when an entry on the menu is selected, that the handles are to be fetched (but they are already in the handles structure passed in!), and the available menu items are to be changed to be the handles structure.
Your code should look more like,
function pushbutton1_Callback(hObject, eventdata, handles)
filename = uigetfile({'*.xlsx'}, 'File Selector');
[~, ~, raw] = xlsread(filename);
[r, c] = find(strcmp(raw, 'oz'));
z = cat(1, raw{r+1:end, c});
zcell = cat(1, raw(r+1:end, c));
[row, ~] = find(diff(z) ~= 0);
zlook = [zcell(row); zcell(end)];
newz(:, 1) = zlook(:, 1);
newz = cell2mat(newz);
set(handles.popupmenu1, 'String', newz);
function popupmenu1_Callback(hObject, eventdata, handles)
allvalues = get(hObject, 'String');
selection_idx = get(hObject, 'Value');
current_value = str2double( allvalues{selection_idx) );
Call_Some_Function_Here(current_value);
Christina Paris
Christina Paris 2017 年 8 月 7 日
Thank you. That worked. Only difference is I changed current_value = str2double( allvalues{selection_idx) ); to
current_value = str2double( allvalues(selection_idx,:) );
because the numbers in allvalues were of class char.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2017 年 8 月 7 日

編集済み:

2017 年 8 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by