How can I save axes data for recall without keeping the handle?

5 views (last 30 days)
What I am trying to do:
I would like to create a figure with 1 plot that a user can interact with and when finished, that same figure transforms into a 2x2 subplot. I want the original figure that the user was interacting with to show in the upper left, then populate resultant data in the other 3 subplots. I have figured out a sort of work around where I open a second figure and copy the axes handle's children to there, but I want to do it all in one plot. Perhaps it is not possible but I am determined that someone here has an answer.
This is the gist of my code:
plot1 = figure;
axis([-1000 1000 -1000 1000]);
axis square
grid on;
hold on;
plot1Axes = gca(plot1)
subplot(2, 2, 1);
copyobj(plot1Axes.Children, gca);
When copyobj runs there is an error "Invalid or deleted object". I know this is because gca returns an axes object handle. Can I somehow save off the data in that handle without losing it when it is closed or when subplot overwrites the current axes?
For reference too, this is that workaround I discussed:
plot1 = figure;
axis([-1000 1000 -1000 1000]);
axis square
grid on;
hold on;
plot1Axes = gca(plot1)
plot2 = figure
subplot(2, 2, 1);
copyobj(plot1Axes.Children, gca);

Accepted Answer

Voss
Voss on 8 Mar 2024
You can use the syntax subplot(m,n,p,ax) to use an existing axes as a subplot in the same figure.
Example:
% create a figure and plot something
f = figure;
plot([-500 500],[-500 500])
axis([-1000 1000 -1000 1000]);
axis square
grid on;
hold on;
% ... then user interaction happens ...
% now convert the axes to a subplot axes
subplot(2,2,1,f.CurrentAxes);
% subsequent subplot calls work as usual
subplot(2,2,2)
axis square
subplot(2,2,3)
axis square
subplot(2,2,4)
axis square

More Answers (1)

Andrew Reibold
Andrew Reibold on 8 Mar 2024
Edited: Andrew Reibold on 8 Mar 2024
To achieve the desired behavior in MATLAB, you can follow these steps:
  1. Create a single figure with the initial plot.
  2. Allow the user to interact with the plot.
  3. Once the user is finished interacting, split the figure into a 2x2 subplot.
Here's a sample code snippet that demonstrates this:
% Step 1: Create a single figure with the initial plot
figure;
plot1Axes = subplot(1, 1, 1);
axis(plot1Axes, [-1000 1000 -1000 1000]);
axis square;
grid on;
hold on;
% User interaction (you can customize this part based on your requirements)
% For example, let the user click on the plot or perform some actions.
% Once the user is done interacting, proceed to step 2.
% Step 2: Split the figure into a 2x2 subplot
subplot(2, 2, 1); % Upper-left subplot
copyobj(plot1Axes.Children, gca);
% Additional subplots (you can customize these based on your data)
subplot(2, 2, 2);
% Add your data or additional plots in this subplot
subplot(2, 2, 3);
% Add your data or additional plots in this subplot
subplot(2, 2, 4);
% Add your data or additional plots in this subplot
This code creates a single figure, lets the user interact with the plot, and then transforms the figure into a 2x2 subplot with the original plot in the upper-left subplot. The copyobj function copies the children of the original axes to the new subplots. Customize the code based on your specific requirements for user interaction and data visualization.
EDIT : Response to Comment: "Did you run that code? It will throw an error "Invalid or deleted object." when you call the line below. I do not think you can get around that handle getting deleted by subplot"
Oops! One way to resolve this issue might be to store the handles of the plotted objects in a variable and then use those handles with copyobj. A modified version of the code is below.
By storing the handles of the plotted objects in the plotHandles variable before creating the subplots, that might ensure that these handles are still valid when using copyobj. Hoping this might resolve the "invalid or deleted object" error.
% Step 1: Create a single figure with the initial plot
figure;
plot1Axes = subplot(1, 1, 1);
axis(plot1Axes, [-1000 1000 -1000 1000]);
axis square;
grid on;
hold on;
% User interaction (you can customize this part based on your requirements)
% For example, let the user click on the plot or perform some actions.
% Once the user is done interacting, proceed to step 2.
% Store the handles of the plotted objects
plotHandles = plot1Axes.Children;
% Step 2: Split the figure into a 2x2 subplot
subplot(2, 2, 1); % Upper-left subplot
copyobj(plotHandles, gca);
% Additional subplots (you can customize these based on your data)
subplot(2, 2, 2);
% Add your data or additional plots in this subplot
subplot(2, 2, 3);
% Add your data or additional plots in this subplot
subplot(2, 2, 4);
% Add your data or additional plots in this subplot
  1 Comment
groupo
groupo on 8 Mar 2024
Did you run that code? It will throw an error "Invalid or deleted object." when you call the line below. I do not think you can get around that handle getting deleted by subplot
copyobj(ENPlot.Children, gca);

Sign in to comment.

Categories

Find more on Graphics Object Identification in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!