How to store data from pushbutton within a nested function?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hunter Stevenson
il 25 Mag 2018
Commentato: Stephen23
il 27 Mag 2018
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 :)
0 Commenti
Risposta accettata
Stephen23
il 25 Mag 2018
Modificato: Stephen23
il 25 Mag 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 Commenti
Stephen23
il 27 Mag 2018
"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.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
