getappdata / setappdata does not work for me?
이전 댓글 표시
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...
채택된 답변
추가 답변 (1개)
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
Roger Breton
2022년 2월 16일
Cris LaPierre
2022년 2월 16일
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.
카테고리
도움말 센터 및 File Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!