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 件のコメント
採用された回答
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
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Maintain or Transition figure-Based Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!