Subplot disappeared after getting its handle
4 次查看(过去 30 天)
显示 更早的评论
I have a figure consisting of five subplots and I want to save or open one of them in another figure. For some reason, whenever I tried to get the handle with the first line. That subplot specifically just turned empty and the new figure contains only an empty axis too. Any help on selecting a subplot would be appreciated.
h_subplot = subplot(5,1,2);
figure;
h_newaxis = copyobj(h_subplot, gcf);
set(h_newaxis, 'Position', get(0,'DefaultAxesPosition'));
0 个评论
采纳的回答
Constantino Carlos Reyes-Aldasoro
2023-5-9
Ok, I have now seen that the error is in the object that you are trying to copy, it should NOT be the subplot, but the actual object that you display in that subplot. Try the following:
figure
h1_subplot = subplot(5,1,2);h1_ax = imagesc(rand(16));
figure
h2_subplot = subplot(5,1,4);
h2_newaxis = copyobj(h1_ax,h2_subplot );
Hope that now the problem should be solved.
3 个评论
Constantino Carlos Reyes-Aldasoro
2023-5-19
In that case, you will need to find it through handles and children. For example, take this code:
figure
subplot(3,2,1); plot(1:10,1:10,'bo');
subplot(3,2,2); plot(1:20,1:20,'rx');
subplot(3,2,3); plot(1:30,1:30,'md');
subplot(3,2,4); plot(1:40,1:40,'ys');
subplot(3,2,5); plot(1:50,1:50,'gd');
Now, we use gcf to get the figure and store this in the handle h0
h0=gcf;
get(h0)
As you can see there are 5 children in the handle, you can then access these
h1 = h0.Children(2);
get(h1)
You will need to play a bit to find which is the axes that you want to copy, but this will work.
If you want to know more about handles and how to create publication quality images, I can recommend my book:
Biomedical Image Analysis Recipes in MATLAB: For Life Scientists and Engineers
https://www.wiley.com/en-sg/Biomedical+Image+Analysis+Recipes+in+MATLAB%3A+For+Life+Scientists+and+Engineers-p-9781118657553
Hope this solves your problem, or at least points you in the right direction.
更多回答(1 个)
Constantino Carlos Reyes-Aldasoro
2023-5-6
The problem is the order in which you are passing the commands, you create a subplot in a figure, then you call for a new figure and then you use gcf. Try like this
h_fig = figure;
h_subplot = subplot(5,1,2);
h_newaxis = copyobj(h_subplot, h_fig);
set(h_newaxis, 'Position', get(0,'DefaultAxesPosition'));
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!