tags:

views:

69

answers:

3

Hello,

I write a script in matlab, which produces figures out a set of data.

The figures are supposed to rather similar with respect to formating, and each of them is ought to display a set of data ( it is graph embedded in a 3d-domain ). Each of these figures is ought furthermore to display a set of particles within that 3d-domain.

So i would like to create the first figure, then make several copies of it, and put in the data sets. However, I do not know, how I can create clones of a figure in Matlab in a simple way.

Do you know, how I can clone figures?

The online documentation didn't help. Thank you very much!

+3  A: 

You can put the code you use to generate your base figure into a function, then call that function multiple times to create multiple copies of your base figure. You will want to return the graphics handles for those figures (and probably their axes) as outputs from the function in order to modify each with a different set of plotted data. For example, this function makes a 500-by-500 pixel figure positioned 100 pixels in from the left and bottom of the screen with a red background and one axes with a given set of input data plotted on it:

function [hFigure,hAxes] = make_my_figure(dataX,dataY)
  hFigure = figure('Color','r','Position',[100 100 500 500]);  %# Make figure
  hAxes = axes('Parent',hFigure);                              %# Make axes
  plot(hAxes,dataX,dataY);  %# Plot the data
  hold(hAxes,'on');         %# Subsequent plots won't replace existing data
end

With the above function saved to an m-file on your MATLAB path, you can make three copies of the figure by calling make_my_figure three times with the same set of input data and storing the handles it returns in separate variables:

x = rand(1,100);
y = rand(1,100);
[hFigure1,hAxes1] = make_my_figure(x,y);
[hFigure2,hAxes2] = make_my_figure(x,y);
[hFigure3,hAxes3] = make_my_figure(x,y);

And you can add data to the axes of the second figure like so:

plot(hAxes2,rand(1,100),rand(1,100));
gnovice
+6  A: 

MATLAB's built-in function copyobj should also work. Here's an example:

peaks;
f2=copyobj(gcf,0);
Doresoom
+1: Forgot about COPYOBJ.
gnovice
+2  A: 

A convenient way to generate a function that sets all the parameters such that the figure (as in @gnovice's post) will look just right is to create the first figure with all the data (including the 3D points) and all the formatting, and then choose from the FILE-menu the command GENERATE M-FILE... (have a look at the tutorial linked here).

This creates a function that you can save on the Matlab path, and which you can later call with new input to create an exact clone of the first figure with new data.

Jonas

related questions