필터 지우기
필터 지우기

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

조회 수: 14 (최근 30일)
Mark Lupas
Mark Lupas 2020년 4월 20일
댓글: dpb 2020년 4월 21일
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
Mark Lupas
Mark Lupas 2020년 4월 21일
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.
dpb
dpb 2020년 4월 21일
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???

댓글을 달려면 로그인하십시오.

채택된 답변

Muthu
Muthu 2020년 4월 20일
편집: Muthu 2020년 4월 20일
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
Muthu
Muthu 2020년 4월 21일
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
dpb
dpb 2020년 4월 21일
"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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by