How to store data from pushbutton within a nested function?

2 次查看(过去 30 天)
I feel like this is a super easy fix, but I cannot seem to figure out how to store a variable within a nested function...Here is my function so far
data_labels and step_labels are variable length cell arrays, while input_labels is a variable length array of sequential numbers between 1 & 10.
My issue is I can get my pushbutton to correctly output 'vals' via the display, but when I try storing vals outside of the push_call function, I cannot seem to get it to overwrite the zeros array.
function vals = MyGUI2(data_labels,input_labels,step_labels)
num_inputs = max(input_labels) - min(input_labels) +1;
num_steps = max(size(step_labels));
components_index = step_labels;
h.table = uitable('units', 'pixels', 'position',...
[10, 10, num_inputs*110, num_steps*40],...
'columnname', data_labels,...
'columnformat', {'logical'}, 'ColumnEditable', true,...
'rowname', step_labels,...
'data', true(numel(components_index),num_inputs));
% Creates a pushbutton to save data
vals = zeros(num_steps,num_inputs);
h.push = uicontrol('style','pushbutton','units','pixels',...
'position',[num_inputs*200/4,50,80,40],'string','Done',...
'callback',@push_call);
function vals = push_call(varargin)
vals = get(h.table,'data'); % determines which boxes are checked
disp(vals) % just displaying your values that you have selected
end
% need to strore vals somehow!
end
Thanks in advance :)

采纳的回答

Stephen23
Stephen23 2018-5-25
编辑:Stephen23 2018-5-25
Add waitfor right at the end of the main function (the one that you call from the command window). E.g.:
...
waitfor(h.push)
end
Basically your confusion is because functions are synchronous, but GUI's are asynchronous. You cannot call the synchronous function which initiates a GUI and expect it to obtain data from asynchronous triggers that occur randomly in the GUI. You can pause the synchronous code until the asynchronous code is not going to run any more (i.e. the GUI is closed) (which is what waitfor does), or store the data in some persistent memory or something similar.
  5 个评论
Hunter Stevenson
Hunter Stevenson 2018-5-26
Hey! I got it to work out! I am not entirely sure why, but in my nested function I had vals as the output argument, and it was not storing ‘vals’
I noticed you didn’t have any output arguments for your nested function, so I deleted it and it worked out. Any idea why?
Thanks again for your help!
Stephen23
Stephen23 2018-5-27
"I noticed you didn’t have any output arguments for your nested function, so I deleted it and it worked out. Any idea why?"
What does "it worked out" mean? If you delete the nested function nestfun then my code will throw an error when the callback is triggered. Callback functions do not use output arguments.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by