getappdata works to load a variable that was never set?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Betzalel Fialkoff
el 30 de Sept. de 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 comentarios
Respuesta aceptada
Jan
el 30 de Sept. de 2017
Editada: Jan
el 30 de Sept. de 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 comentarios
Jan
el 1 de Oct. de 2017
Editada: Jan
el 1 de Oct. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Maintain or Transition figure-Based Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!