Clear Filters
Clear Filters

How to share plot from one GUI to another using setappdata / getappdata approach?

2 views (last 30 days)
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
Adam
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
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".

Sign in to comment.

Answers (1)

Jan
Jan on 12 Mar 2019
Edited: Jan on 12 Mar 2019
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 Interactive Control and Callbacks in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!