setappdata/getappdata: problem of data recovery

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

What do you mean by 'the main function'? How is this linked to where the pushbutton is?
the main function is the function I run (not the GUI function that controls the GUI and that contains all callback functions). It is saved in the currend folder.
The main function calls the GUI function. (When we create a GUI interface, we save it with a name, and then, we can write this name in an other script/function to run the GUI. In my case, the other function is the main function)
Is it enough clear?
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
With your explication, I changed my program:
1. Main function started
2. figure to create the dialog
3. uiwait(gcf);
4. User selects checkboxes in the dialog
5. User hits the "Valider_Callback" control (what ever this is)
6. setappdata(0, 'Capteurs', Capteurs)
7. uiresume(gcbf);
8. Capteurs5=getappdata(0,'Capteurs');
9. Main function ends
It has solved my problem of "delay". But simultaneously with my GUI interface, it opens an empty figure (called Figure 1). How can I avoid this?
thanks in advance
I have the solution of my problem !!
My error was at step 3:
h = name_of_my_gui;
uiwait(h);
instead of
uiwait(gcf);

Sign in to comment.

Answers (1)

I assume the program flow looks like this:
  1. Main function started
  2. figure to create the dialog
  3. getappdata(0, 'Capteurs')
  4. Main function ends
  5. User selects checkboxes in the dialog
  6. User hits the "Valider_Callback" control (what ever this is)
  7. 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:
  1. Main function started
  2. figure to create the dialog
  3. Main function ends
  4. User selects checkboxes in the dialog
  5. User hits the "Valider_Callback" control (what ever this is)
  6. 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

I have a question about your 6th step : "Now call what should happen with the Capteurs data directly from the callback."
What do you mean by "from the callback" ?
( And yes, you're right, your 1st plan is what I did... and I understand my error)
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.
yes, it's what i thought, thanks :)

Sign in to comment.

Asked:

on 4 Dec 2017

Commented:

on 7 Dec 2017

Community Treasure Hunt

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

Start Hunting!