MATLAB: How to combine multiple columns of a table into one array to plot

plottable

I am creating a table by reading through a file, the table contains the following:
Var1 Var2 Var3 Var4 Var5
9728.015877288 34.21601656 -85.26319543 6120.27 0.71
9728.100523585 34.19514296 -85.29652054 2121.33 2.60
9728.162613281 33.67647324 -83.90831590 2601.09 2.36
9728.178688810 34.17953039 -85.35196003 8092.13 4.57
9728.187776872 34.17839105 -85.35730262 8066.19 1.53
Var1 is time in seconds, and I am converting it to hh:mm:ss:SSS. Each row of the table is a "source" that contains the data Var1, Var2, Var3, Var4, Var5. I want to plot converted time vs source, where the user can input the ranges of each variable.
[FileName,PathName]=uigetfile(':','Select the LYLOUT file');
T=readtable(fullfile(PathName,FileName));
figure('units','normalized','outerposition',[0 0 1 1]);
prompt1 = {'(1) Enter the lower range of latitude where ' + string(min(T.Var2)) + ' is the minimum: ','(2) Enter upper range of latitude where ' + string(max(T.Var2)) + ' is the maximum: '};
dlgtitle1 = 'Latitude Range Input';
dims = [1 35];
answer1 = str2double(inputdlg(prompt1,dlgtitle1,dims));
prompt2 = {'(1) Enter the lower range of longitude where ' + string(min(T.Var3)) + ' is the minimum: ','(2) Enter upper range of longitude where ' + string(max(T.Var3)) + ' is the maximum: '};
dlgtitle2 = 'Longitude Range Input';
answer2 = str2double(inputdlg(prompt2,dlgtitle2,dims));
prompt3 = {'(1) Enter the lower range of altitude where ' + string(min(T.Var4)) + ' is the minimum: ','(2) Enter upper range of altitude where ' + string(max(T.Var4)) + ' is the maximum: '};
dlgtitle3 = 'Altitude Range Input';
answer3 = str2double(inputdlg(prompt3,dlgtitle3,dims));
prompt4 = {'(1) Enter the lower range of reduced chi squared where ' + string(min(T.Var5)) + ' is the minimum: ','(2) Enter upper range of reduced chi squared where ' + string(max(T.Var5)) + ' is the maximum: '};
dlgtitle4 = 'Reduced chi squared Range Input';
answer4 = str2double(inputdlg(prompt4,dlgtitle4,dims));
idx = T.Var2 >= answer1(1) & T.Var2 <= answer1(2) & T.Var3 >= answer2(1) & T.Var3 <= answer2(2) & T.Var4 >= answer3(1) & T.Var4 <= answer3(2) & T.Var5 >= answer4(1) & T.Var5 <= answer4(2);
timeUTC = seconds(T.Var1);
timeUTC.Format = 'hh:mm:ss.SSS';
plot(timeUTC(idx), [T.Var2(idx), T.Var3(idx), T.Var4(idx), T.Var5(idx)])
ylim([0 inf]);
grid minor;
xlabel('UTC Time');
ylabel('Sources');
title('Time vs Sources');
But instead of combining Var2, Var3, Var4 and Var5 into a variable it plots all of the variables at once. How would I plot the time vs source?

Best Answer

Hello Mark,
From the code, i understand that you need to plot the variables with respective index for selective index of time. (may be use the logic - loop of plots. might work)
t = [ 0 1 2 3 4 5 ]
v1 = [ 0 2 4 6 8 10 ]
v2 = [ 0 3 6 9 12 15 ]
v3 = [ 0 4 8 12 16 20 ]
var = [v1; v2; v3]
hold on
for n = 1: length(t)
plot(t(n), [var(1,n), var(2,n), var(3,n)],'o')
end
A short version:
vars = [T.Var2; T.Var3; T.Var4; T.Var5] % Assuming Var2, Var3, Var4, Var5 are row vectors
hold on
for n = 1: length(vars)
plot(timeUTC(idx), vars(n, idx))
end
hold off
Hope this helps.