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

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

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.
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?
@Rik Wisselink I'm calling the second GUI in pushbutton of first GUI. In axes1 there is a 2d plot which I want to get in second GUI.
@Dennis I'm trying to pass a plot.
This line:
IM=double(getimage(handles.axes1))
is returning:
>> GUI1
IM =
[ ]
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)
@Rik Wisselink I was not using breakpoints but now I've confirmed it by using breakpoints also it does not makes any difference. The variable IM remains empty. Actually I think I'm unable to store the plot in axes1 in a variable. Any solution??
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
@Dennis by your appraoch its giving this error:
Attempt to reference field of non-structure array.
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.
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')
@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.
I'm using matlab 2013b.
This is the complete code in GUI1 pushback button:
% Coordinates Input
x1 = [0 0.12 0.36 0.6];
y1 = [0.05 0.1 0.1 0.03];
% Inputs
noc = 4; % Input
% Cross section ploting
plot(x1,y1,'b','LineWidth',2),grid on , hold on
plot(x1,-y1,'b','LineWidth',2)
line([x1(1) x1(1)],[y1(1) -y1(1)],'LineWidth',2,'Color',[0 0 1]),hold on,line([x1(noc) x1(noc)],[y1(noc) -y1(noc)],'LineWidth',2,'Color',[0 0 1])
hold on,xlim([-0.1 0.7]),hold on,ylim([-0.2 0.2])
% Booms Ploting
plot(x1,y1,'r.','MarkerSize',18)
hold on
plot(x1,-y1,'r.','MarkerSize',18)
hold on
for i = 1:1:noc
a = i;
b = num2str(a);
str1 = 'B';
text(x1(i),y1(i)*1.5,[str1,b])
c = num2str( (noc) + i);
text(x1(i),-y1(i)*1.5,[str1,c])
hold on
end
IM=double(getimage(handles.axes1))
setappdata(0,'IM',IM);
GUI2
And this is the complete code in GUI2 Pushback button:
IM=getappdata(0,'IM')
axes(handles.axes1)
imshow(IM)
GUI1 output figure:
GUI2:
Here in GUI2 I want to get the plot of GUI1. Kindly, Simply tell me how can I do this?
Now I've shared the complete code so anyone can understand what is the actual problem and how it can be solved.
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.
@Jan
Can you please write the proper code which i have to add and also tell where I have to add that code?
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.
@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)

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

Tags

Asked:

on 11 Mar 2019

Commented:

Jan
on 13 Mar 2019

Community Treasure Hunt

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

Start Hunting!