MATLAB: Legend when plotting with while

legendlegend plotmultiple graphsplotplotting

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

Best Answer

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