How to plot multiline with variable inputs

I have vibration data from several components, and I'm trying to create a multi line plot that allows the user to select multiple variables versus time. I can easily script a multi line plot, but I want to make the number of dependent variables flexible, rather than static.
Instead of scripting "plot(x, y1, x, y2, ..., x, yn), I'd like to pass a variable as an index that references the table where the x and y values are stored.
For example: plot(x, vars(:,3), x, vars(:,4)..., x, vars(:,n))
{where x = vars(:,1) or vars(:,2), which are independent variables in my data table}
Is it possible to make the plot function dynamically sized based on the user input?
Thanks in advance

3 件のコメント

KSSV
KSSV 2018 年 2 月 2 日
It can be done......
gshockxc
gshockxc 2018 年 2 月 2 日
Any suggestions?
Greg
Greg 2018 年 2 月 2 日
Comma separated list expansion.

サインインしてコメントする。

回答 (3 件)

ANKUR KUMAR
ANKUR KUMAR 2018 年 2 月 2 日

1 投票

Up to my understanding of your question, I think you are struggling to plot.
You can try this one. Sorry, if I misunderstood your question.
A={rand(1,10),rand(1,10),rand(1,10),rand(1,10)}
col={'g','k','b','r'}
for ii=1:4
plot((1:10),A{ii},col{ii})
hold on
end

2 件のコメント

gshockxc
gshockxc 2018 年 2 月 2 日
Ankur, thanks for the help. This is getting me a lot closer. I want to make "col" dynamically sized. My thought was to create arrays of ones, and then use "deal" to populate the arrays to be graphed, based on user selections.
Is there a good way of doing this, or is this equivalent to making dynamic variables?
ANKUR KUMAR
ANKUR KUMAR 2018 年 2 月 3 日
I didn't get you. What exactly you want to make modification?

サインインしてコメントする。

gshockxc
gshockxc 2018 年 2 月 3 日
編集済み: gshockxc 2018 年 2 月 3 日

0 投票

I was over complicating it. This works.
for i=1:ny
plot(vars{:,x},vars{:,y(1,i)})
i = i+1;
hold on
end

2 件のコメント

Greg
Greg 2018 年 2 月 3 日
Do not increment your loop variable explicitly.
gshockxc
gshockxc 2018 年 2 月 3 日
Thanks for the tip. Corrected.

サインインしてコメントする。

Stephen23
Stephen23 2018 年 2 月 3 日
編集済み: Stephen23 2018 年 2 月 3 日

0 投票

There is no point in making this so complicated: no slow ugly loop or complex code is required. All you need is
plot(x, vars)
because plot will plot the columns of vars against x. You can easily add indexing as required, e.g.:
plot(vars(:,2),vars(:,4:end))

3 件のコメント

gshockxc
gshockxc 2018 年 2 月 3 日
'vars' is a table. The reason I did it this way is because I have user input, where columns can be selected from 'vars', not necessarily consecutive, and not all columns in 'vars' will be plotted. 'y' is an array with the column numbers in 'vars' that are selected from 'listdlg'
Stephen23
Stephen23 2018 年 2 月 3 日
編集済み: Stephen23 2018 年 2 月 3 日
@gshockxc: nothing in my answer requires that the plotted columns must be consecutive. This is MATLAB so just use indexing:
y = [4,6,9]; % pick your columns
plot(vars(:,2),vars(:,y))
gshockxc
gshockxc 2018 年 2 月 4 日
I understand your point. But this is the error I get when trying to plot from 'vars' that way. 'vars' is Class table.
Error using tabular/plot Too many input arguments.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

質問済み:

2018 年 2 月 2 日

コメント済み:

2018 年 2 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by