To programmatically set axis boundaries there are a few useful commands:
axis([0 30 0 8]); %Sets all four axis bounds
or
xlim([0 30]); %Sets x axis limits
ylim([0 8]); %Sets y axis limits
To only set one of the two x limits I usually use code like this:
xlim([0 max(xlim)]); %Leaves upper x limit unchanged, sets lower x limit to 0
This takes advantage of xlim
s zero input argument calling convention, which returns an array of the current x limits. The same works with ylim
.
Note that all of these commands apply to the current axis, so if you are creating subplots you will need to perform the scaling call once per axis as you build up your figure.
Another useful fatures is the linkaxes
command. This dynamically links the axis limits of two plots, including for programmatic resize commands like xlim
and UI operations like pan and zoom. For example:
a(1) = subplot(211),plot(rand(10,1), rand(10,1)); %Store axis handles in "a" vector
a(2) = subplot(212),plot(rand(10,1), rand(10,1)): %
linkaxes(a, 'xy');
axis([0 30 0 8]); %Note that all axes are now adjusted together
%Also try some manual zoom, pan operations using the UI buttons.
Looking at your code, post edit, your use of the plotmatrix
function is complicating things. plotmatrix
appears to create its own axes to work in, so you need to capture those handles and adjust them. (Also, in the future take h = zeros(..)
out of the loop).
To get the handles to the plotmatrix
created axes, use the second return argument, like this: [~, hAxes]=plotmatrix(current_rpm,current_torque);
. Then collect those for future use.
Finally, the axis
, xlim
, ylim
commands all act on the current axis, (see gca
). However the plotmatrix
axes are never current, so the axis
command has not been affecting them. You can specify the axis to act on, like this: axis(hAxis, [0 30 0 8]);
.
Putting this all together (an adding some variable definitions to get your code to execute), and this is what it looks like:
%Define some dummy variables
current_rpm = rand(20,1)*30;
current_torque = rand(20,1)*8;
num_bins = 12;
%Loop to plot, collecting generated axis handles into "hAllAxes"
hAllAxes = [];
for i = 1:num_bins;
subplot(4,3,i);
[~, hCurrentAxes]=plotmatrix(current_rpm,current_torque);
hAllAxes = [hAllAxes hCurrentAxes]; %#ok
end
linkaxes(hAllAxes,'xy');
axis(hAllAxes,[0 30 0 8]);