How do I create a multiline plot using values from a table?

For example, I have a table:
User Date1 Date2 ....(I have 500+ dates)
{'A'} 1 6 ...
{'B'} 2 8 ...
{'C'} 3 9 ...
{'D'} 4 11 ...
The dates would be located in the x-axis and the values would be in the y-axis. Each user would be a separate line on that graph. I was thinking of converting it to an array and then attempting to plot that but that method does not seem to be working since I have different types of data in this table. What would be the most efficient way of doing this?

4 件のコメント

Nora Khaled
Nora Khaled 2020 年 11 月 12 日
so for each user you want diffrent line and data1 is the x axis then for Data2 ~ Data500 are the in the y axis ?
Aryan Cross
Aryan Cross 2020 年 11 月 12 日
Yes. Each user would have a different line. For example for user A, as time progresses from Date 1 to Date 2 in the x axis, the y value would jump from 1 to 6.
Subhadeep Koley
Subhadeep Koley 2020 年 11 月 12 日
Aryan Cross what all "different types of data" your table has?
In order to plot a particular value (for a certain user and date), the value needs to be numeric. And if values are numeric then converting them to array before plotting seems an efficient way.
Aryan Cross
Aryan Cross 2020 年 11 月 12 日
I have cells in my first column but the rest is double.

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

回答 (1 件)

Nora Khaled
Nora Khaled 2020 年 11 月 12 日

0 投票

If I understand the problem correctly this might help
% creating tabel exampel
col1 = {'A';'B';'C';'D';'E'};
col2 = [1:1:5]';
col3 = randi(5,5,1);
col4 = randi(5,5,1);
col5 = randi(5,5,1);
col6 = randi(5,5,1);
my_table = table(col1,col2,col3,col4,col5,col6);
%plot data for each user
[r,c]=size(my_table);
for i=2:1:r
plot(my_table{i,2:end});
hold on;
end
legend(my_table{:,1});

8 件のコメント

Aryan Cross
Aryan Cross 2020 年 11 月 12 日
That looks better, thank you! Is there a way to get my column variables (Date1 to Date500) spaced intermittently along my graph instead of having just numbers?
Nora Khaled
Nora Khaled 2020 年 11 月 12 日
編集済み: Nora Khaled 2020 年 11 月 12 日
add this line:
set(gca,'xticklabel',my_table.Properties.VariableNames)
Nora Khaled
Nora Khaled 2020 年 11 月 12 日
or this to control the text angle:
set(gca,'XTickLabel',my_table.Properties.VariableNames, 'XTickLabelRotation',45)
Aryan Cross
Aryan Cross 2020 年 11 月 12 日
Hmm..I'm not sure what is happening but it is including User as it's first point on the y-axis. It is also only countring the 1st 3 column variables when minimized and 6 when it is maximized instead of going all the way till the end.
Nora Khaled
Nora Khaled 2020 年 11 月 12 日
to remove the "user" use:
set(gca,'XTickLabel',my_table.Properties.VariableNames(2:end), 'XTickLabelRotation',45)
But I don't get what you mean by "t is also only countring the 1st 3 column variables when minimized and 6 when it is maximized instead of going all the way till the end."
Aryan Cross
Aryan Cross 2020 年 11 月 12 日
Aryan Cross
Aryan Cross 2020 年 11 月 12 日
In the example, the x-axis goes from col2 to col6 and restarts from col2 again. I would like it to go from col2 all the way to colx without repeating
Nora Khaled
Nora Khaled 2020 年 11 月 13 日
Ok... try this:
set(gca,'XTick',1:length(my_table.Properties.VariableNames(2:end)),'XTickLabel',my_table.Properties.VariableNames(2:end), 'XTickLabelRotation',45)
Also, change the loop vaiable to start from 1 :
for i=1:1:r
plot(my_table{i,2:end});
hold on;
end

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

カテゴリ

製品

リリース

R2020a

タグ

質問済み:

2020 年 11 月 12 日

コメント済み:

2020 年 11 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by