How to change the axis location for multiple subplots ?

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?

Respuestas (2)

Jan
Jan el 14 de Dic. de 2017
Editada: Jan el 14 de Dic. de 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 comentarios

omar khater
omar khater el 14 de Dic. de 2017
Editada: omar khater el 14 de Dic. de 2017
It doesn't change the result
Jan
Jan el 15 de Dic. de 2017
@omar: I cannot guess what you have done. I tried to guess what "I try to change the axis location for 2 subplots" means already, but perhaps you meant something else. Maybe you made a type when implementing my suggestion.
Please explain the problem more specifically.
As you see in the attached image
I want the subplot 2,3 to be as 1
and here is the code
n1 = -5:5;
x1 = 2*impseq(-2,-5,5) - impseq(4,-5,5);
ax=subplot(2,2,1);
stem(n1,x1);
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
title('Sequence in Problem 2.1a');
xlabel('n1'); ylabel('x(n1)');
ax.Box = 'off';
ax.Layer = 'top';
n2 = 0:20;
x_1 = n2.*(stepseq(0,0,20)-stepseq(10,0,20));
x_2 = 10*exp(-0.3*(n2-10)).*(stepseq(10,0,20)-stepseq(20,0,20));
x2 = x_1+x_2;
ax2 = subplot(2,2,3);
stem(n2,x2);
ax2.XAxisLocation = 'origin';
ax2.YAxisLocation = 'origin';
title('Sequence in Problem 2.1b');
xlabel('n2');
ylabel('x(n2)');
n3 = 0:50;
x3 = cos(0.04*pi*n3)+0.2*randn(size(n3));
ax3=subplot(2,2,2);
stem(n3,x3);
ax3.XAxisLocation='origin';
ax3.YAxisLocation = 'origin';
title('Sequence in Problem 2.1c')
xlabel('n3');
ylabel('x(n3)');
@omar khater: While "I want the subplot 2,3 to be as 1" is clear to you, the readers have to guess, what this means. Remember that I do not have the faintest idea about what you want.
As you see in the attached image, I want...
The image shows, what you get, but not what you want instead.
The posted code does exactly, what is expected. The locations of the axes are moved to the corresponding origins - this is on the left for the upper right axes, and on the left bottom corner for the lower axes. So please explain exactly what does not appear as you want.
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

Iniciar sesión para comentar.

Yoseph
Yoseph el 6 de Oct. de 2022
n = [-5:5];
x = 2*impseq(-2,-5,5)-impseq(4,-5,5);
stem(n,x); title('Sequence in Problem 2.1a')
xlabel('n'); ylabel('(n)');

Etiquetas

Preguntada:

el 14 de Dic. de 2017

Respondida:

el 6 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by