How can I plot vectors that are not of the same length in one plot so that they overlap?
10 views (last 30 days)
Show older comments
Hello everyone!
I have a problem plotting two vectors that are of different lengths (5 and 100) into one figure, so that they overlap. You might rephrase the problem in 'How can I, if possible at all, get two differently scaled x-axis in the same plot?' I already tried assigning different axis numbers, like
x1 = 0:1:5;
A1 %is a 5x1 vector
x2 = 0:20:200;
A2 %is a 100x1 vector
plot(x1,A1,x2,A2)
but that only got me the "Error using plot. Vectors must be the same length." message. (EDIT: I now learned, why, but still have the same problem, see attached file.) I also tried with hold:
x = 0:1:100;
A1, A2 %as before
plot(x, A1)
hold on
plot(x, A2)
hold off
which at least plotted the two graphs, but in the same scale (i.e. A2 the way it should be, but A1 only in the very small first 5% of the figure...) because it used the 1 to 100 x-axis that doesn't suit a vector that goes from 1 to 5. Does anyone know how to 'stretch' the vector or re-scale the axis or of any other solution to compare the plotted vectors?
Thanks in advance!
2 Comments
Matt J
on 19 Nov 2017
Neither of these approaches should have worked. x has 101 elements which is different in length from both A1 and A2.
Matt J
on 19 Nov 2017
Note that x1 as you have defined it in your first approach is not of length 5 to match A1, nor is x2 of length 100 to match A2.
>> x1=0:1:5; x2=0:20:200; whos x1 x2
Name Size Bytes Class Attributes
x1 1x6 48 double
x2 1x11 88 double
Answers (3)
Matt J
on 19 Nov 2017
Edited: Matt J
on 19 Nov 2017
How about
semilogx( 1:numel(A1), A1, 1:numel(A2), A2);
2 Comments
Matt J
on 19 Nov 2017
Edited: Matt J
on 19 Nov 2017
though I don't exactly know what semilogx is because I don't have much experience with coding in MatLab
The doc command will help with that
>> doc semilogx
but that also returned me the 'Error using semilogx. Vectors must be the same lenght.' message...
No reason it should have. Worked fine for me.
If you are not using 1:numel(A1) as in my example, make sure your x vectors are the same length as their corresponding A vectors.
Anna M
on 19 Nov 2017
Edited: Anna M
on 20 Nov 2017
1 Comment
Matt J
on 20 Nov 2017
It's not clear to me how they are meant to be compared. To be comparable, they surely have to be on a common x-axis, and if they are, they surely cannot "overlap" because you don't have the same x-data available for each.
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!