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

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),
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),
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);
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 Exchange에서 Code Execution에 대해 자세히 알아보기

질문:

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