Whats wrong with the following commands?
Show older comments
% The plotted x-axis goes from 0 to 1, instead of 20. Why?
ax = gca;
ax.xlim = [0 20];
drawnow
3 Comments
@Johannes
on 30 Sep 2015
I have a small example for you
time=[0:2:24];
data=[5;6;12;18;15;16;18;15;10;9;8;7;7];
figure(1);
plot(time,data,'r','LineWidth',2);
ax=gca;
ax.XLim = [0 20];
grid on;
title('test','fontsize',12,'fontweight','bold');
xlabel('x-axis','fontsize',12,'fontweight','bold');
ylabel('y-axis','fontsize',12,'fontweight','bold');
Regards, John
the cyclist
on 30 Sep 2015
This code worked as expected for me. See attached.

Mr M.
on 30 Sep 2015
Accepted Answer
More Answers (1)
the cyclist
on 30 Sep 2015
This code worked for me:
figure plot(rand(7,5)) ax = gca; ax.XLim = [0 20]; drawnow
(I changed the capitalization of ax.XLim.)

6 Comments
Mr M.
on 30 Sep 2015
the cyclist
on 30 Sep 2015
disp('Create the figure window with default axes, and "hold" so that later axes add on, rather than replace.')
figure
hold on
pause(2)
disp('Resize the axes')
ax = gca;
ax.XLim = [0 20];
pause(2)
disp('Add a plot')
plot(rand(7))
pause(2)
disp('Bye bye')
Mr M.
on 30 Sep 2015
Edited: Walter Roberson
on 1 Oct 2015
the cyclist
on 30 Sep 2015
Strange. Works for me. The output of those commands is attached below. (R2015a on Mac OS X 10.10.5.)

Mr M.
on 1 Oct 2015
I hope this fixes your Problem. With hold(ax,'on') it is not possible for the plot command to update the axis.
figure
ax = gca;
ax.XLim = [0 20];
hold(ax,'on')
plot([],[])
Regards, John
Categories
Find more on Labels and Annotations 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!