How to store data from pushbutton within a nested function?

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

 Accepted Answer

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 Comments

Ah thanks! Works perfectly!
Oh no, I spoke too soon. It is still not storing the values despite adding the waitfor function...
It works for me: I created an MWE function to test it:
function val = testfun()
val = [];
fgh = figure('Position',[400,400,200,100]);
hnd = uicontrol(fgh,'Style','pushbutton',...
'Callback',@nestfun,'String','click me');
waitfor(hnd)
function nestfun(~,~)
val = 1:3;
end
end
Then called it from the command line:
>> val = testfun() % Did not click button. Closed figure.
val =
[]
>> val = testfun() % Clicked button (callback runs). Closed figure.
val =
1 2 3
You can see that by clicking the button the callback was called and clearly changed the value of val in the main function's workspace. In both cases val was simply returned as an output argument from the main function. The figure looks like this:
This is an intuitive, robust, and simple way to merge asynchronous code back into a synchronous script or function. Note that it would be very bad practice to write a GUI that magically accesses variables in the base workspace: the best practice is to load/create variables withing the GUI, work with them inside the GUI callback workspace/s, before saving them or returning them to the base workspace as I showed above.
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!
"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.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!