How to change the axis location for multiple subplots ?

15 views (last 30 days)
omar khater
omar khater on 14 Dec 2017
Commented: omar khater on 17 Dec 2017
I try to change the axis location for 2 subplots
when I entered this code
n = [-5:5];
x = 2*impseq(-2,-5,5) - impseq(4,-5,5);
subplot(2,2,1);
stem(n,x);
ax=gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
title('Sequence in Problem 2.1a');
xlabel('n'); ylabel('x(n)');
ax.Box = 'off';
ax.Layer = 'top';
n = [0:20];
x1 = n.*(stepseq(0,0,20)-stepseq(10,0,20));
x2 = 10*exp(-0.3*(n-10)).*(stepseq(10,0,20)-stepseq(20,0,20));
x = x1+x2;
subplot(2,2,3);
stem(n,x);
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
title('Sequence in Problem 2.1b');
xlabel('n'); ylabel('x(n)');
It changes it for the first subplot only ..why is this?

Answers (1)

Jan
Jan on 14 Dec 2017
Edited: Jan on 14 Dec 2017
Because your
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
concerns the same object "ax". Try this:
ax2 = subplot(2,2,3);
stem(n,x);
ax2.XAxisLocation = 'origin';
ax2.YAxisLocation = 'origin';
Note that
ax = subplot(2,2,1);
is safer than;
subplot(2,2,1);
stem(n,x);
ax=gca;
If the user clicks around in the GUI during stem works, the current axes replied by gca is changed. Better catch the handles directly from the function, which creates the objects instead of searching them afterwards by gca or gcf.
  5 Comments
omar khater
omar khater on 17 Dec 2017
I want the axis of plots 2,3 to be in the middle of the graph as in plot 1
The image shows that the axis in the plot 3 doesn't change as I expect

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!