Variable number of plots in the same figure

7 次查看(过去 30 天)
Hello, I would like to automate the construction multiple plots. Manually I have been writing:
plot(x1, y1, '-b',x2, y2, '-b',x3, y3, '-b',x4, y4, '-b')
How can I do that programatically for many more (x,y) ? Instead of 4, I may have 30 or 40. I tried this (unsuccessfully) with the hope that I could built my plotString in a loop structure:
plotString='x1, y1, ''-b'',x2, y2, ''-b'',x3, y3, ''-b'',x4, y4, ''-b''';
plot(plotString)
Thanks !
PS: x and y are vectors not scalars. Besides they all have different sizes so they are stored in cell arrays: e.g. xArray is [1x4 cells] and contains [1x79 double],[1x82 double],[1x82 double],[1x91 double] I am just running a test with 4 vectors but I have to handle way more than that so I have to find a way to "feed" my arrays to the plot function.
  4 个评论
MattH
MattH 2018-4-24
errr I should have mentioned that my variables are vectors, not scalars. I tried to convert them into cell arrays but it cannot be a one-cell array as you suggest. I am lost
Stephen23
Stephen23 2018-4-25
编辑:Stephen23 2018-4-25

" I tried to convert them into cell arrays but it cannot be a one-cell array as you suggest"

I did not write that you should use a "a one-cell array", I wrote that you could use "one cell array" (which will certainly have more than one cell). You did not show us any code, or give any reason why it is impossible to use one cell array, so presumably you made some mistake in your code. If you want help with this then you need to give us something to work with (our mindreading abilities are a bit rusty these days).

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2018-4-24
Don't make the data arrays with sequential names; instead use arrays (by column per each variable) and then PLOT() can handle them all in one fell swoop; you simply use a logical addressing vector to pick and choose which column(s) you want plotted for any given figure.
See <Variables A1 A2....A10> for why is a very bad idea to code that way and some other alternatives besides just array (although almost certainly that's the right solution here).
  3 个评论
dpb
dpb 2018-4-24

All we have to go on is what you write so when you use Xn, Yn, we have to presume that's what you mean, not something else entirely.

From doc plot

plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.

  • If X and Y are both vectors, then they must have equal length. The plot function plots Y versus X.
  • If X and Y are both matrices, then they must have equal size. The plot function plots columns of Y versus columns of X.
  • If one of X or Y is a vector and the other is a matrix, then the matrix must have dimensions such that one of its dimensions equals the vector length. If the number of matrix rows equals the vector length, then the plot function plots each matrix column versus the vector. If the number of matrix columns equals the vector length, then the function plots each matrix row versus the vector. If the matrix is square, then the function plots each column versus the vector.

I'll presume you have the second case and want "variables" 1, 2 and 5 from an array with at least 5 columns.

ixPlt=[1 2 5];  % define the columns (variables) to plot
hL=plot(x(:,ixPlt),y(:,ixPlt);

That's it...altho I'll grant the examples aren't as illuminating as they could be.

MattH
MattH 2018-4-27
of course, what I was missing was the indexing ! Thanks
I found a temporary workaround using a loop and "hold on" but this is much neater
Thanks a lot dpb !

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by