フィルターのクリア

Legend when plotting with while

1 回表示 (過去 30 日間)
Viktor G.
Viktor G. 2020 年 2 月 28 日
コメント済み: Cameron B 2020 年 2 月 29 日
Hey guys,
so i am trying to plot multiple variables from an excel file and I can't get around with it. What would be the best way to do this, since what I've done will never get the legend correct? Please help
clc
clear all
T = readtable('Book1.xlsx');
Time=T.DateTime;
ColumnYouNeed = input('Which variable would you like to plot: ','s');
mask = ismember(T.Properties.VariableNames,ColumnYouNeed);
NewTable = table2array(T(:,mask));
plot(Time,NewTable)
hold on
legend(ColumnYouNeed)
a=input('Would you like to plot another variable? ','s');
while a=='yes'
ColumnYouNeed = input('Which variable would you like to plot: ','s');
mask = ismember(T.Properties.VariableNames,ColumnYouNeed);
NewTable = table2array(T(:,mask));
plot(Time,NewTable)
legend(ColumnYouNeed)
a=input('Would you like to plot another variable? ','s');
end

採用された回答

Cameron B
Cameron B 2020 年 2 月 28 日
This may work.
clc
clear all
T = readtable('Book1.xlsx');
Time=T.DateTime;
ColumnYouNeed = input('Which variable would you like to plot: ','s');
mask = ismember(T.Properties.VariableNames,ColumnYouNeed);
NewTable = table2array(T(:,mask));
plot(Time,NewTable)
hold on
legend(ColumnYouNeed)
a=input('Would you like to plot another variable? ','s');
cc=2;
while a=='yes'
ColumnYouNeed(cc) = input('Which variable would you like to plot: ','s');
mask = ismember(T.Properties.VariableNames,ColumnYouNeed);
NewTable = table2array(T(:,mask));
plot(Time,NewTable)
legend(ColumnYouNeed)
a=input('Would you like to plot another variable? ','s');
cc = cc +1;
end
  2 件のコメント
Viktor G.
Viktor G. 2020 年 2 月 29 日
編集済み: Viktor G. 2020 年 2 月 29 日
I am not sure why but i doesn't work and i keeps giving this error, even though they are the same size. Can you please try it with a simple table?
Error using plot
Vectors must be the same length.
Error in probaodstranata (line 17)
plot(Time,NewTable)
Cameron B
Cameron B 2020 年 2 月 29 日
I got this to work. You’ll have to adapt it, but it should be easy enough. The only issue would be putting decimals, but you wouldn’t be doing that anyway.
clc
clear
aa=gcf;
delete(aa)
ColumnYouNeed = input('Which variable would you like to plot: ','s');
ColumnYouNeedNum = str2double(ColumnYouNeed);
xx=0:10;
plot(xx,xx*ColumnYouNeedNum)
hold on
legend(ColumnYouNeed)
a=input('Would you like to plot another variable? ','s');
cc=2;
while strcmp(a,'yes') == 1
ColumnYouNeed(cc) = input('Which variable would you like to plot: ','s');
ColumnYouNeedNum(cc) = str2double(ColumnYouNeed(cc));
plot(xx,xx*ColumnYouNeedNum(cc))
legend(ColumnYouNeed(:))
a=input('Would you like to plot another variable? ','s');
cc=cc+1;
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by