setappdata/getappdata: problem of data recovery
Show older comments
Hello everybody !
I have a problem to retrieve data from GUI.
In the GUI function, inside a pushbutton callback (that allows Matlab to retrieve all data "put" in the GUI interface), I retrieve values (0 or 1) the user entered from checkbox buttons.
This is the pushbutton callback code: (NB: RenA, RenB, EtrA, EtrB are the tags of the checkboxes)
function Valider_Callback(hObject, eventdata, handles)
Capteurs=struct;
Capteurs.etrA =get(handles.EtrA,'Value');
Capteurs.etrB =get(handles.EtrB,'Value');
Capteurs.renA =get(handles.RenA,'Value');
Capteurs.renB =get(handles.RenB,'Value');
setappdata(0,'Capteurs',Capteurs);
In the main function, I write:
Capteurs5=getappdata(0,'Capteurs');
What I get is only the variable "Capteurs", but empty. I don't get 0 or 1 !

Then I re-run the code (the code calls the GUI interface, asks the user to fill the checkboxes, and then the user clicks on the pushbutton to submit his response).
And what I get is the response the user fills the 1st time I run the code. And not what the user enters the 2nd time. There is a delay of "one run"...

I don't understand why there is a " delay " to retrieve the data. How can I fix it? It's an issue for me.
If someone could help me... I would be grateful.
I use Matalb 2012b
Thanks in advance, and I hope you understand my problem :)
5 Comments
Adam
on 4 Dec 2017
What do you mean by 'the main function'? How is this linked to where the pushbutton is?
Minialoe
on 4 Dec 2017
Adam
on 5 Dec 2017
Do you have any kind of uiwait instruction then if you are running a function, launching a GUI and then expecting the function to wait while you get a response from the GUI?
If not then the main function will just launch the GUI and run to completion before the user has entered any values, so the appdata will only be set after that, which is why it is only available next time you run the main function.
doc uiwait
doc waitfor
are useful for stopping the code flow to wait for a response from a user
Minialoe
on 7 Dec 2017
Minialoe
on 7 Dec 2017
Answers (1)
Jan
on 5 Dec 2017
0 votes
I assume the program flow looks like this:
- Main function started
- figure to create the dialog
- getappdata(0, 'Capteurs')
- Main function ends
- User selects checkboxes in the dialog
- User hits the "Valider_Callback" control (what ever this is)
- getappdata(0, 'Capteurs', Capteurs)
This means, that the main function does not wait for the GUI. One method would be uiwait mentioned by Adam. Then the main function waits for the dialog to be closed (or for another event). Or you can restructure the code:
- Main function started
- figure to create the dialog
- Main function ends
- User selects checkboxes in the dialog
- User hits the "Valider_Callback" control (what ever this is)
- Now call what should happen with the Capteurs data directly from the callback.
This is the usual method GUIs work. Instead of transporting the data through the ApplicationData of the root object and letting the main function wait for an event or the data, the dialog is the main function. Here the data are defined by user inputs and the further calculations are started. This is easier and avoids indirections.
3 Comments
Minialoe
on 7 Dec 2017
Jan
on 7 Dec 2017
An example in the code (assuming that you work with GUIDE):
% The callback of e.g. a "Start" button:
function ButtnCallback(hObject, EventData, handles)
Capteurs = handles.Capteurs; % Get data from handles struct
Result = YourCalculator(Capteurs);
plot(Results)
end
I cannot be more precise, because I do not know, what you want to do with the data. But instead of starting it from the command window, you can start it from inside the callback function. Then the base workspace and the root object are not polluted by global variables and you can run multiple instances of the GUI at the same time also without the danger of collisions.
Minialoe
on 7 Dec 2017
Categories
Find more on Kinect For Windows Sensor 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!