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

 Respuesta aceptada

Stephen23
Stephen23 el 25 de Mayo de 2018
Editada: Stephen23 el 25 de Mayo de 2018
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 comentarios

Hunter Stevenson
Hunter Stevenson el 25 de Mayo de 2018
Editada: Hunter Stevenson el 25 de Mayo de 2018
Ah thanks! Works perfectly!
Oh no, I spoke too soon. It is still not storing the values despite adding the waitfor function...
Stephen23
Stephen23 el 25 de Mayo de 2018
Editada: Stephen23 el 25 de Mayo de 2018
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Preguntada:

el 25 de Mayo de 2018

Comentada:

el 27 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by