How can I save axes data for recall without keeping the handle?
5 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
采纳的回答
Voss
2024-3-8
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
更多回答(1 个)
Andrew Reibold
2024-3-8
编辑:Andrew Reibold
2024-3-8
To achieve the desired behavior in MATLAB, you can follow these steps:
- Create a single figure with the initial plot.
- Allow the user to interact with the plot.
- 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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!