How to set different y limits for multiple subplots?

216 visualizaciones (últimos 30 días)
Ibro Tutic
Ibro Tutic el 30 de Mayo de 2017
Respondida: Giuseppe Naselli el 21 de Feb. de 2018
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.

Respuestas (2)

Giuseppe Naselli
Giuseppe Naselli el 21 de Feb. de 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

Santhana Raj
Santhana Raj el 31 de Mayo de 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.

Community Treasure Hunt

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

Start Hunting!

Translated by