# Looping through Axes on figure created using subplot

조회 수: 19 (최근 30일)
Jason 2024년 1월 8일
댓글: Voss 2024년 1월 8일
Hello, I have a figure with 5 subplots on.
hf1=figure('position',pos,'Name','4LineScan'); % pos defined elsewhere
ax1=subplot(1,5,1); ax2=subplot(1,5,2); ax3=subplot(1,5,3);
ax4=subplot(1,5,4); ax5=subplot(1,5,5);
I then want to perform the same operation on different data and plot on one of each of the axes. Rather than have my code sequencial, Im thinking of doing it in a loop, but am trying to be able to get each axes in a loop.
I thought this might be one way
hChild = findobj(hf1, 'Type','Axes' )
h1=hChild(1)
But I want to be sure that on iteration 1 of the loop it picks out ax1, and then ax2 etc. Is there a way to do this
Thanks
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Dyuman Joshi 2024년 1월 8일
As you are working with R2022b, (imo) it would be better to use tiledlayout and add plots to tiles using a for loop.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Voss 2024년 1월 8일
hf1=figure('position',pos,'Name','4LineScan'); % pos defined elsewhere
% store the axes in an array:
ax = [ ...
subplot(1,5,1) ...
subplot(1,5,2) ...
subplot(1,5,3) ...
subplot(1,5,4) ...
subplot(1,5,5) ...
];
% then it's easy to access the ii-th axes in a loop:
for ii = 1:5
% do whatever you want with ax(ii)
end

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Jason 2024년 1월 8일
Actually I have got this to work
A=[ax1,ax2,ax3,ax4,ax5];
for i=1:5
AX=A(i)
% do what ever I want
end
It would still be nice to know how to get the actual axes name if thats possible
##### 댓글 수: 4이전 댓글 2개 표시이전 댓글 2개 숨기기
Jason 2024년 1월 8일
Joe, so "ax1", "ax2" etc, where I have deifned it earlier as ax1=subplot(1,5,1)
Joshi, it was neither.
Voss 2024년 1월 8일
@Jason: What are you going to do with ax1, ax2, ax3, ..., that you can't do with ax(1), ax(2), ax(3), ... (in my version) or A(1), A(2), A(2), ... (in your version)?
You can always construct a string that says 'ax1' or whatever
for ii = 1:5
str = "ax"+ii;
disp("Working with axes "+str);
end
Working with axes ax1 Working with axes ax2 Working with axes ax3 Working with axes ax4 Working with axes ax5

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by