How to share plot from one GUI to another using setappdata / getappdata approach?
Show older comments
Hi, I’m trying to pass plot / Image from GUI1 to GUI2 but unable to do this. I prefer setappdata / getappdata approach. I’m creating GUIs using GUIDE.
This is the code which I'm writing in GUI1:
IM=double(getimage(handles.axes1))
setappdata(0,'IM',IM);
GUI2 % calling GUI2
And this is the code in GUI2:
IM=getappdata(0,'IM')
axes(handles.axes1)
imshow(IM)
But in command window it shows:
>> GUI1
IM =
[]
IM =
[]
And in GUI2 I'm not able to get the plot.
I've searched it a lot on youtube also but I'm not getting the solution. So if anyone knows then kindly share the solution of this problem as soon as possible.
16 Comments
Rik
on 11 Mar 2019
When are you calling that second GUI? Are you certain the referenced axes contains an image?
If h is not an image or does not contain an image, then I is empty.
Dennis
on 11 Mar 2019
Can you please check if this line actually returns anything:
IM=double(getimage(handles.axes1))
The return of GUI seems to tell you otherwise. Are you trying to pass a plot or an image?
Shoaib Bilal
on 11 Mar 2019
Shoaib Bilal
on 11 Mar 2019
Rik
on 11 Mar 2019
The way you formatted your reply doesn't look like you are using breakpoints. Can you confirm you are indeed not using them? (and if you are not, please try using them to see what the state of your objects is when you try to retrieve the variable)
Shoaib Bilal
on 12 Mar 2019
Dennis
on 12 Mar 2019
I am not sure if you can use getimage to obtain data from a plot. Try this instead:
IM=handles.axes1.Children
IM.XData
IM.YData
Shoaib Bilal
on 12 Mar 2019
All the evidence seems to point to the fact that
handles.axes1
contains no graphics objects, let alone an image. And scanning up through the question and comments I don't see anywhere where you show the code that puts an image in handles.axes1 before trying to access it.
Dennis
on 12 Mar 2019
Which Matlab version are you using? Older Matlab versions (before 2014b?) might need this synthax:
IM=get(handles.axes1,'Children')
get(IM,'XData')
get(IM,'YData')
Jan
on 12 Mar 2019
@Shoaib: Then you can use the debugger to find out, what IM is. We cannot guess this and we cannot run you code, because you did not provide a minimal working example, which reproduces the problem.
Using the root object to transport data has the same cruel disadvantages as using global variables:
setappdata(0,'IM',IM);
Sharing the data directly without using the root 0 is a much better idea.
The actual problem seems to be here already:
IM=double(getimage(handles.axes1))
If getimage(handles.axes1) does not reply the expected image data, the rest of the code must fail. So use the debugger, set a break point in this line and check, how you can get the wanted data.
Shoaib Bilal
on 12 Mar 2019
Edited: Shoaib Bilal
on 12 Mar 2019
Jan
on 12 Mar 2019
You do not display any image at all. This would be done by imshow or the image command. Without an image, getimage replies the empty matrix, as Rik has mentioned yesterday already.
You want to copy the children of the axes, or the complete axes to the new figure. This is done by copyobj.
Shoaib Bilal
on 12 Mar 2019
Adam
on 13 Mar 2019
You would be better off, in general, to put your plotting code into a function, taking the arguments it needs to create the plot and then just pass this data around to wherever you want (e.g. the 2nd GUI) and call your plotting function on the 2nd GUI rather than moving and copying GUI objects themselves around.
Jan
on 13 Mar 2019
@Shoaib Bilal: "Can you please write the proper code which i have to add and also tell where I have to add that code?" - Sorry, this is not possible. I cannot guess, what your full current code is. You have posted a small piece of it only. The code I have posted in my answer is proper already and it has to be inserted in the code, which you have called "GUI2 Pushback button".
Answers (1)
To avoid troubles, define the Parent property when drawing in a GUI:
...
Ax = handles.axes1;
plot(Ax, x1,y1,'b','LineWidth',2)
grid(Ax, 'on');
hold(Ax, 'on');
plot(Ax, x1,-y1,'b','LineWidth',2)
line(Ax, [x1(1) x1(1)],[y1(1) -y1(1)],'LineWidth',2,'Color',[0 0 1])
line(Ax, [x1(noc) x1(noc)],[y1(noc) -y1(noc)],'LineWidth',2,'Color',[0 0 1])
xlim(Ax, [-0.1 0.7]);
ylim(Ax, [-0.2 0.2]);
...
You need to call hold on once only.
If the tag of the GUI is 'GUI1' , use this to copy the axes1 of the first GUI to the 2nd GUI:
function AXZ_Callback(ObjectH, EventData, handles)
GUI1H = findobj(allchild(groot), 'flat', 'Figure', 'Tag', 'GUI1');
GUI1handles = guidata(GUI1H);
GUI2H = ancestor(ObjectH, 'figure'); % Or the corresponding handles field
copyobj(GUI1handles.axes1, GUI2H);
end
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
