Repeatedly adding subplots to new figure via GUIDE

7 views (last 30 days)
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

Joseph Cheng
Joseph Cheng on 26 Feb 2015
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
Joseph Cheng
Joseph Cheng on 27 Feb 2015
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.
Alex
Alex on 27 Feb 2015
That did it. Thank you so much for your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!