getappdata / setappdata does not work for me?
Ältere Kommentare anzeigen
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...
Akzeptierte Antwort
Weitere Antworten (1)
Cris LaPierre
am 16 Feb. 2022
Bearbeitet: Cris LaPierre
am 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 Kommentare
Roger Breton
am 16 Feb. 2022
Cris LaPierre
am 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.
Kategorien
Mehr zu Lighting, Transparency, and Shading finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!