How to set different y limits for multiple subplots?
조회 수: 142 (최근 30일)
이전 댓글 표시
I have 12 subplots that I am trying to plot, but when I plot them the axes are all messed up. I would like to set the y-axis based on the min and max value of the data that I am plotting, but I get this error when trying to set the limits
Subscript indices must either be real positive integers or logicals.
This is my code:
for i=1:length(idx)
subplot(4,3,i);
plot(DLG.Data{1,1}(:,1),DLG.Data{1,1}(:,idx(i)));
xlim([0 250]);
min = min(DLG.Data{1,1}(:,idx(i)));
max = max(DLG.Data{1,1}(:,idx(i)));
ylim([min max]);
end
The xlimits are always the same, the only thing that change are the y-limits.
댓글 수: 0
답변 (2개)
Giuseppe Naselli
2018년 2월 21일
if you use the handle of each sub plot you will have much more freedom (and less issues :) ) for example
subplot1 = subplot(4,3,1) %get the handle of this subplot/axis
plot(x,y,'Parent',subplot1) % (see the use of parent-child relationship?)
xlim(subplot1,[min max]); % assign your limits only to that specific subplot
repeat in a loop or for each one as you wish. Using handles makes life much easier G
댓글 수: 0
Santhana Raj
2017년 5월 31일
Your mistake is to use the function name as a variable. "min"
The first loop will work. where you use the min function to get the value and store it in min. This overwrites the function min. The next iteration will throw the error. Same happens for Max also.
Try with different variable name for min and max....it should work fine.
참고 항목
카테고리
Help Center 및 File Exchange에서 Subplots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!