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!