getappdata / setappdata does not work for me?
5 views (last 30 days)
Show older comments
I am trying to improve my code by adopting 'better' programming practices. I want to start using into the setappdata functions and looked into the documentation and, on the face of it, it seems like a no-brainer but there are some frustrating catches I'm not getting? Here are the first few lines of my current script :
global Diam ListeImages ah handleImage ValuePopup ColorCircle_ab cb_sRGB;
figImage = figure;
Tmp = 1;
setappdata(figImage,'ValuePopup1',Tmp);
cb_sRGB = true;
As I understand it, to use setappdata requires some existing 'graphic object' in the input arguments? Fine. So, let's create an empty figure object, called 'figImage'. Then, pass it some 'element' like 'ValuePopup', along with its 'value'. So far so good? (no compiler complain, here) As you can see, this is declared on the very first lines of the script so it's as 'global' as I can get. Then, further down, in some function, I have this code :
function DisplayImage(imageIn)
global img ListeImages Inner ValuePopup ColorCircle_ab cb_sRGB;
Tmp = getappdata(figImage,'ValuePopup1');
Naturally, this does not work? I get a nice 'Invalid or deleted object' error. What am I missing?
The documentation says: "The graphics object must be accessible from within the functions you plan to store and retrieve the data.". How on earth should I make the 'object' accessible from within my function? I tried adding figImage to my globals (but that would be stupid?) like so :
global figImage Diam ListeImages ah handleImage ValuePopup ColorCircle_ab cb_sRGB;
and then, in my function definition :
function DisplayImage(imageIn)
global figImage img ListeImages Inner ValuePopup ColorCircle_ab cb_sRGB;
But as soon as the compiler gets to this line :
set(figImage, 'Name', 'AfficheImage_2022_02_03.m');
I still get an 'Invalid or deleted object.' error. Somehow, I'm not going the correct way about this.
Sadly, there isn't much documentation on this function for me to chew on...
0 Comments
Accepted Answer
Jan
on 16 Feb 2022
Edited: Jan
on 16 Feb 2022
After the line
figImage = figure;
the variable figImage contains the handle of the figure. If you want to use this handle in a function, you have to provide it as input argument:
function main
figH = figure;
myVar = rand
setappdata(figH, 'myVar', myVar); % Store variable in figure
subFunction(figH);
end
function subFunction(figH)
myVar = getappdata(figH, 'myVar');
disp(myVar);
plot(1:10);
end
Usually the variables stored by setappdata() are used by callbacks of the figure:
function main
figH = figure;
myVar = rand;
setappdata(figH, 'myVar', myVar); % Store variable in figure
uicontrol(figH, 'Style', 'pushbutton', 'String', 'Click!', ...
'Callback', @myCallback);
end
function myCallback(buttonH, EventData)
figH = ancestor(buttonH, 'figure');
myVar = getappdata(figH, 'myVar');
disp(myVar);
end
Alternatively you could provide the handle of the figure as 3rd input argument:
... as above
uicontrol(figH, 'Style', 'pushbutton', 'String', 'Click!', ...
'Callback', {@myCallback, figH});
function myCallback(buttonH, EventData, figH)
% Not needed then: figH = ancestor(buttonH, 'figure');
The idea of setappdata is to store data inside a figure (or any other gui element), instead of using global variables. But if you use the root object, this is exactly the same as globals:
setappdata(groot, 'myVar', rand)
This has the same püroblem as globals: All functions can overwrite this variable. e.g. if you run multiple instances of the same GUI or if you run third-party software, which uses the same names by accident.
In your case the messge "Invalid or deleted object" means, that the variable figImage is still existing, but contains the handle of an already deleted figure or it is empty. Seeing your posted code, I cannot guess, why this is the case.
0 Comments
More Answers (1)
Cris LaPierre
on 16 Feb 2022
Edited: Cris LaPierre
on 16 Feb 2022
I thnk the better way is to pass the figure object into your function as an input parameter. I think your issue is due to you setting a property 'ValuePopup' but getting a property named 'Valuepopup1'. Probably just a simple typo.
Here is a simple example.
figImage = figure;
Tmp = 1;
setappdata(figImage,'ValuePopup',Tmp);
% call the function
DisplayImage(figImage)
function DisplayImage(figImage)
getTmp = getappdata(figImage,'ValuePopup')
end
2 Comments
Cris LaPierre
on 16 Feb 2022
Your approach of using a global also works, and if you hadn't had that typo, you never would have posted your question. It's just that globals are often not the best way to share variables.
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!