How to set different y limits for multiple subplots?

217 ビュー (過去 30 日間)
Ibro Tutic
Ibro Tutic 2017 年 5 月 30 日
回答済み: Giuseppe Naselli 2018 年 2 月 21 日
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.

回答 (2 件)

Giuseppe Naselli
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

Santhana Raj
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.
  1 件のコメント
dpb
dpb 2017 年 5 月 31 日
good catch, read right over it...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by