Repeatedly adding subplots to new figure via GUIDE

Hello,
I have created a GUI in GUIDE that plots a large amount of data on an axes within the GUI. What I would like to do is essentially use that axes as a preview axes, if that makes sense, and once it is deemed satisfactory by the user, it can be copied to a different figure outside of the GUI into its own subplot. I would like to be able to do this several times, such that I basically add new subplots to the other figure as I approve of the preview axes in the GUI. I have a pushbutton in the GUI called 'copyButton' with the idea that I can press this button, and my GUI axes and accompanying graph will be copied into a new subplot within the other figure, while also preserving any previously added subplots.
As I've mentioned, there is a tremendous amount of data, all stored in various multi-tiered structs within the GUI handles, so I'd like to avoid re-executing the plot command if possible. I think my key roadblock right now is just how one would copy the GUI plot over to the other figure into its own subplot without clearing the other subplots.
If anyone could provide any additional help, I'd be very appreciative. Let me know if there is any additional detail needed.
Thanks!

 Accepted Answer

without coding it into a gui, this uses copyobj. Seeing how this works you can run the first section up until i define figh2 = figure(2); then use the second section using the run section button or ctrl+enter while in the second part multiple times to keep adding to the original subplot figure
clear all
close all
%generate subplots
figh1 = figure(1);
for ind=1:3
axh(ind) = subplot(3,1,ind); %handles for each subplot
plot(rand(10,3)); %generate some random plot
title(['this is plot ' num2str(ind)]);
end
%%------section to add to existing subplot----------
figh2 = figure(2);
axh2 = axes,stem(rand(10,3)); %get handle of plot to be added
title(['this chart to be added to subplot']);
figh3 = figure(3);
%figure out new sizes based on current number of subplots +1
for ind=1:length(axh)+1
axh3(ind) = subplot(length(axh)+1,1,ind);
%obtain positioning and sizes
tempposition(ind,:) = get(axh3(ind),'position');
end
close(figh3);%close it since we don't need it anymore
figure(figh1);%go back to subplot figure that you want to add to
for ind =1:length(axh)
%update each subplot handle with new position
set(axh(ind),'position',tempposition(ind,:));
end
%copy new plot to the subplot figure and add to subplot database
axh(end+1)=copyobj(axh2,figh1);
close(figh2)
%update the new subplot entry with the right position
set(axh(end),'position',tempposition(end,:));

7 Comments

Thank you for the response!
Just to be clear, when you say, "without coding it into a gui, this uses copyobj", are you implying that this cannot be implemented in the GUI and needs a standalone script (or two)? Or do you mean that I need to simply adjust accordingly to implement into my GUI?
oh no, i just didn't have time time to code it into the GUI and having it still understandable. One of the things is that my GUI implementation would be probably very different. This can be adjusted to fit with a GUI. One thing that caused me to do it this way is i didn't have the time or mental capacity at the moment to deal with GUIDE handles and keeping it all straight while trying to come up with a way to do this easily.
Ok, I've been trying to include it into my GUI, but when it copies the graph of interest, it positions it in the very bottom corner as seen here:
However, when I try to run it using only your code, it works just fine. I haven't changed much other than adding in "handles." to a few places. If it helps, the tag for the axes in my GUI is "axes1", so I tried changing
axh2 = axes,stem(rand(10,3)); %get handle of plot to be added
to
axh2 = handles.axes1
Based on the tiny bit of the graph title shown in the bottom corner, it seems to have copied the correct thing, but I can't move it from that position.
hmm.... after the copy portion put a breakpoint just after the figure you show. At that point i'd start debugging in the workspace by looking at the positions of both tempposition(4,:) of where we're about to put it, and the position of axh(end) by using get(axh(end),'position'). And see if those make sense. Perhaps in your adaptation something wasn't over written? or set properly.
one thing did come to mind, in the same debugged state i'd check to see what
get(axh(1),'units') returns versus what get(axh(end),'units'). it might be a simple fix where the units are not the same and the fix is to add
set(axh(end),'units','normalized');
The units weren't, in fact, the same, so setting them to normalized did the trick. Thanks!
Is there a way I can easily replace the subplots originally generated? I used the first part of the code in the GUI opening function, such that figh1 is created with three random subplots. So, while I can successfully add more graphs, the original random subplots remain.
So how do should I modify this portion:
figh1 = figure(1);
for ind=1:3
axh(ind) = subplot(3,1,ind); %handles for each subplot
plot(rand(10,3)); %generate some random plot
title(['this is plot ' num2str(ind)]);
end
to copy the first of the desired GUI plots into the first subplot instead of the random plots?
ok so what i did to update it is comment out/delete the initial for loop to generate. As we need a axh defined as we can't do axh(end) = copied plot we can make an empty axh (axh=[];).. one of the things that you could implement is checking if axh exists though something like this
if ~exist('axh') % i think you used handles.axh and you'll need the '' as exists takes a string.
axh=[];
end
So with that axh can be added to by the 2nd part of my initial example and you can delete axh (ie handles.axh) if you want to clear the subplot figure and start over.
That did it. Thank you so much for your help!

Sign in to comment.

More Answers (0)

Asked:

on 26 Feb 2015

Commented:

on 27 Feb 2015

Community Treasure Hunt

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

Start Hunting!