getappdata works to load a variable that was never set?

3 vues (au cours des 30 derniers jours)
Betzalel Fialkoff
Betzalel Fialkoff le 30 Sep 2017
Modifié(e) : Jan le 1 Oct 2017
Hi I've been using getappdata/setappdata to share variables across different callbacks in my GUI.
Basically what happens is that the user calls a one of the callbacks to load an image into the application and i used setappdata, so that the image can be called somewhere else.
the problem i discovered while debugging on the line of code : >> mask=getappdata(0,'mask');%recieve the mask
if i read this line even without having saved the variable "mask" (with setappdata) somehow the variable mask still gets loaded into the workspace.
does anyone know what causes this? how i can correct it? or if there is an alternative to sharing variables across callback
  3 commentaires
Betzalel Fialkoff
Betzalel Fialkoff le 1 Oct 2017
is there some way i can use getappdata without storing it in a root object? if not what is a good alternative?
Stephen23
Stephen23 le 1 Oct 2017
Modifié(e) : Stephen23 le 1 Oct 2017
"is there some way i can use getappdata without storing it in a root object?"
Read the setappdata / getappdata help, which list all of the different graphics object types that you can use.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 30 Sep 2017
Modifié(e) : Jan le 30 Sep 2017
It is a bad style to share data using the root object 0. This has the same problems as using global variables and you find thousands of explanations in this forum and the net about the severe problems caused by globals.
Better share variables of callbacks by storing them in the figure. Then you can e.g. run multiple instances of a GUI without interferences.
Your description is not clear. As far as I understand you run
data = getappdata(0, 'AVariableNotCreatedBefore')
and are surprised that [] is returned. This is exactly, what is defined in the documentation:
help getappdata
... If the application-defined data does
not exist, an empty matrix will be returned in VALUE.
Perhaps you want to check the existence at first:
if isappdata(0, 'AVariableNotCreatedBefore')
data = getappdata(0, 'AVariableNotCreatedBefore');
else
error('Request not existing variable');
end
Or perhaps you have the problem, that the globally set variable is not cleared, when the GUI is closed. This is expected also, because you stored it in the root object. Then the solution is mentioned already: Store the data in the figure and not globally.
  7 commentaires
Jan
Jan le 1 Oct 2017
Modifié(e) : Jan le 1 Oct 2017
@Betzalel: Do you know the so called wrapper function guidata? It calls get/setappdata also internally using the figure handle. This is frequently used to store data locally in the figure:
function YourCallback(hObject, EventData, handles)
handles.Mask = (rand(3) < 0.5); % For example
guidata(hObject, handles); % Store modified handles struct in the figure
end
function AnotherCallback(hObject, EventData, handles)
Mask = handles.Mask;
...
end
guidata determines the parent figure belonging to the object hObject automatically. But you can do this with set/getappdata directly also:
function YourCallback(hObject, EventData, handles)
FigH = ancestor(hObject, 'figure'); % [EDITED, Typo fixed]
Mask = (rand(3) < 0.5); % For example
setappdata(FigH, 'Mask', Mask);
end
function AnotherCallback(hObject, EventData, handles)
FigH = ancestor(hObject, 'figure');
Mask = getappdata(FigH, 'Mask');
...
end
Note that the handles of the figure is contained in the handles struct also. You can check this by using the debugger.
Betzalel Fialkoff
Betzalel Fialkoff le 1 Oct 2017
I found the guidata function while i was waiting to hear back from you.
I believe the 2nd solution (the one with the figures) will be better since i can do it via an external function.
Thank you very much for your assistance!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Maintain or Transition figure-Based Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by