Subplot disappeared after getting its handle
4 ビュー (過去 30 日間)
古いコメントを表示
Shao-Yu Wang
2023 年 5 月 6 日
コメント済み: Shao-Yu Wang
2023 年 5 月 21 日
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!