How to set different y limits for multiple subplots?
147 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Giuseppe Naselli
on 21 Feb 2018
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 Comments
Santhana Raj
on 31 May 2017
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.
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!