error using subplot,index exceed numbers of subplots

조회 수: 17 (최근 30일)
Austin
Austin 2013년 10월 29일
편집: Christopher Jones 2013년 10월 29일
x=1:1:10 for i=2:2:8 y=4*x-3*i subplot(4,1,i) plot(x,y) end
why do i keep getting this error- error using subplot,index exceed numbers of subplots

답변 (2개)

Christopher Jones
Christopher Jones 2013년 10월 29일
편집: Christopher Jones 2013년 10월 29일
Your subplot only has 4x1 subfigures ie 4 with indexes 1,2,3,4
Your index vector i on the other hand tries to access 2,4,6,8.
Two of those indexes (6,8) don't exist and exceed the number of subplots.
To quickly fix the problem just change the for argument to i=1:4 and add a *2 to the y function
figure;
x=1:1:10
for k=1:4
i = k*2;
y=4*x-3*i;
subplot(4,1,k);
plot(x,y) ;
end

Jos (10584)
Jos (10584) 2013년 10월 29일
A general solution that loops over the values using a separate index:
x=1:1:10
value = 2:2:8
N = numel(value)
for k = 1:N
y=4*x-3*value(k)
subplot(N,1,k)
plot(x,y)
title(sprint('Value #%d : %d',k,value(k))) ; % add a title
end

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by