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

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?

3 Comments

"...instead of combining Var2, Var3, Var4 and Var5 into a variable..."
I don't understand...what does the above mean? You've selected a subset of the variables by user input and returned those selected subset variables. What would you expect to plot except four variables for that subset?
I don't see how you could combine four variables into one???
Can you show what a plot of what you envision would look like?
Something along this idea although it doesn't need to be a bar graph.
Where the Y axis is Sources, and X is TimeUTC. Each row of the table is a single "source", and I want to graph them against time, and allow the user to select the bounds of each Var of source. What I did on accident was plot Var2, Var3, Var4 and Var5 on the same graph.
The (or at least one) problem is where does the time come for the subsequent channels--you've got 4X the variable data as the length of the time vector returned by the indexing operation. Are there four simultaneous measurements to plot as groups sequentially or what???

Sign in to comment.

 Accepted 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.

6 Comments

sources = [T.Var2;T.Var3;T.Var4;T.Var5];
hold on
for n = 1:length(sources)
plot(timeUTC(idx),sources(n,idx))
end
I am getting the error
"The logical indices in position 2 contain a true value outside the array bounds.
Error in (line 26)
plot(timeUTC(idx), sources(n,idx))"
T.VarN --> 1D column vector, so sources is also. Then you try to access it as 2D array.
Again, it would certainly help if you would answer the Q? I asked above to explain clearly what you're trying to do -- certainly isn't at all clear to me.
BTW
sources = [T.Var2;T.Var3;T.Var4;T.Var5];
is just
sources = reshape(T{:,2:5},[],1);
Sources is a column vector. In For loop, length(sources) creates length of rows.
For loop should iterated for number of columns.
It can be changed to
sources = [T.Var2;T.Var3;T.Var4;T.Var5];
hold on
for n = 1:length(sources(:,1))
plot(timeUTC(idx),sources(n,idx),'o')
end
Try this out.
Good luck.
It is still giving me the same error.
The logical indices in position 2 contain a true value
outside of the array bounds.
Error in Lylout_parse_2 (line 26)
plot(timeUTC(idx),sources(n,idx),'o')
Below is the program i used to realize the logic.
Please adapt it to your program.
Sorry, i could not help you further.
t = [ 0 1 2 3 4 5 ];
v1 = [ 1 2 4 6 8 10 ];
v2 = [ 2 3 6 9 12 15 ];
v3 = [ 3 4 8 12 16 20 ];
v4 = [ 4 5 10 15 20 25 ];
sources = [v1;v2;v3;v4];
idx = 1; % can be changed according to the index (row) required
hold on
for n = 1:length(sources(:,1))
plot(t(idx),sources(n,idx),'o')
end
"It is still giving me the same error. "
Of course, because you tried the same thing -- addressing a 1D vector as 2D array.
Answer the follow-up question above...your lack of response to needed inputs to diagnose first what the problem description really is thus leading to a coding solution is causing this to go on apparently interminably when it could probably be solved quickly if we could just figure out what it is you think you want, specifically.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Asked:

on 20 Apr 2020

Commented:

dpb
on 21 Apr 2020

Community Treasure Hunt

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

Start Hunting!