getappdata works to load a variable that was never set?

3 次查看(过去 30 天)
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 个评论
Betzalel Fialkoff
Betzalel Fialkoff 2017-10-1
is there some way i can use getappdata without storing it in a root object? if not what is a good alternative?
Stephen23
Stephen23 2017-10-1
编辑:Stephen23 2017-10-1
"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.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2017-9-30
编辑:Jan 2017-9-30
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 个评论
Jan
Jan 2017-10-1
编辑:Jan 2017-10-1
@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 2017-10-1
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by