Clone figure in Matlab - with properties and data

后端 未结 3 2153
粉色の甜心
粉色の甜心 2021-02-20 08:53

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

3条回答
  •  耶瑟儿~
    2021-02-20 09:43

    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));
    

提交回复
热议问题