Reputation: 25
In a piece of code in matlab, I generate a figure in which 5 different subplots appear. I was hoping it is possible to save these subplots to variables so I can later call upon one of these subplots and have the same subplot open in its own figure (So they can each be viewed more accurately if desirable). Unfortunately I have not been able to figure out how to do this myself.
I know a figure as a whole can be saved to a variable in the way described here: https://nl.mathworks.com/matlabcentral/answers/160049-how-to-assign-a-plot-to-a-variable
But this only saves the figure for as long as it stays opened after you have first created it. On top of that, it doesn't say how to do the same for subplots.
I generate my subplots in the following way:
for i = 1:length(figuredata)
subplot(2,3,i); % select subplot
plot(figuredata{1,i},figuredata{2,i},'r.') , grid on; % plot the figure
title(figuretitles{i}); % set title
ylabel('SIN'), xlabel('COS'),grid on; % label the axes
axis([0 16384 0 16384]); axis('square'); % set axis range for x and y
end
Where figuredata is a 2 x 5 cell array containing the data for each of the 5 plots and figuretitles contains the titles of the subplots
Does anyone happen to know how I could achieve what I want?
Upvotes: 2
Views: 1433
Reputation: 125874
One thing you need to understand first is the concept of graphics object handles. In MATLAB, a handle is a reference to a particular graphics object. You use the handle to access and change properties of the object. A handle and the actual object itself should be thought of as separate (but related) entities. For example:
hFigure = figure(); % Create figure window and return handle
clear hFigure % Clear variable containing handle
This creates a figure window and stores the handle to the window in variable hFigure
, then clears the variable. However, the figure still exists, because we only discarded a handle that referred to the object. Alternatively:
hFigure = figure(); % Create figure window and return handle
delete(hFigure); % Delete the graphics object
get(hFigure) % Use handle to try and access the object properties
Error using matlab.ui.Figure/get
Invalid or deleted object.
This creates a figure window, stores the handle to the window in a variable, then deletes the figure. When the object is deleted, the handle is no longer valid, and you get an error if you try to use it.
With this now in mind, there are a few options you have for moving, saving, and redisplaying graphics objects. Assuming you have an existing subplot (i.e. an axes object) that hasn't been closed or deleted, you can reparent the object to a new window by modifying the Parent
property. For example:
hAxes = subplot(2, 2, 1);
plot(hAxes, [1 2], [1 2], 'r');
hFigure = figure();
set(hAxes, 'Parent', hFigure);
This will create a subplot, then move it to a new window. Note that the axes no longer exists in the original window, but it still has the same position, size, etc. that it had. If you want it to appear differently (i.e. a larger plot to fill the new window), you'll have to modify its properties after moving it.
Another alternative is to use the copyobj
function:
hAxes = subplot(2, 2, 1);
plot(hAxes, [1 2], [1 2], 'r');
hFigure = figure();
copyobj(hAxes, hFigure);
This will make a copy of the axes object, so that there are now two independent graphics objects, one in each window.
If you're dealing with a situation where the original figure will be closed, and you want to save a copy of the axes objects so that you can redisplay them later, you can do this with the undocumented (or semi-documented) functions handles2struct
and struct2handle
. Here's an example that creates an axes with a line plotted in it, stores the axes object structure in a .mat file (using save
), then loads the structure and adds it to a new figure:
hAxes = subplot(2, 2, 1);
plot(hAxes, [1 2], [1 2], 'r');
axesStruct = handle2struct(hAxes);
save('Axes_data.mat', 'axesStruct');
clear all;
close all;
load('Axes_data.mat');
hFigure = figure();
hAxes = struct2handle(axesStruct, hFigure);
Upvotes: 3