Plot data vs time color coded by ID

조회 수: 7 (최근 30일)
Jen B
Jen B 2022년 2월 12일
댓글: William Rose 2022년 2월 13일
Hi,
MATLAB newbie here. I'm trying to plot the range in miles of two aircraft from where I am over time on the same plot.
Here is an example of the data from a table named 'data':
'15:02:03' 1 24.492
'15:02:03' 2 25.653
'15:02:04' 1 24.489
'15:02:04' 2 25.650
'15:02:05' 1 24.485
'15:02:05' 2 25.646
'15:02:06' 1 24.482
'15:02:06' 2 25.643
'15:02:07' 1 24.478
'15:02:07' 2 25.639
---------------------------
The variables I'm using are:
Column 1 is data.time
Column 2 is data.ID
Column 3 data.range
I'm able to plot time vs. range using plot(data.time, data.range), but what I need to do is plot each aircraft's range vs. time color coded by data.ID and I thought it would be simpler than it is, but I'm a stumped. I'm pretty sure I need a for loop to loop through data.ID in data but can't figure out the syntax. Thanks for any help!
JB
  댓글 수: 2
Image Analyst
Image Analyst 2022년 2월 12일
Is data.time string or character variables? Or time series variable? Or doubles? It's easiest if they're numerical not strings. Please attach your table in a .mat file with the paperclip icon so we can begin on helping you.
save('answers.mat', 'data')
Jen B
Jen B 2022년 2월 12일
Hi,
I wasn't sure how to attach the file before and it looks like there are a couple of other answers as well but I've attached it anyway. Thanks for the help!

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

채택된 답변

William Rose
William Rose 2022년 2월 12일
편집: William Rose 2022년 2월 12일
I have copied your data into a text file and have attached it.
I notice there are two planes and a distance to each one at each time. The times are strings.
Let's figure out how many planes rhere are, so the code will generalize for a future input data file in which there are 3 or more plane distances at every time. Then lop through th code to make a vector of times and a distance array with time along the rows and a different plane in each column.
Then we will plot time vs dist1 and time vs dist2, with different colors.
T=readtable('jbdata.txt'); %T has strings and numbers
nPlanes=max(T.Var2); %number of planes
nTimes=length(T.Var2)/nPlanes; %number of times
A=zeros(nTimes,nPlanes); %allocate array
for i=1:nTimes
t(i)=T.Var1((i-1)*nPlanes+1);
for j=1:nPlanes
A(i,j)=T.Var3((i-1)*nPlanes+j); %distance
end
end
%If there are more than 2 planes, make next line a for loop over planes
plot(t,A(:,1),'rx',t,A(:,2),'bx'); %plot time vs distance for each plane
legend('Plane 1','Plane 2'); %adjust if >2 planes
xlabel('Time'); ylabel('Distance'); grid on
Try it.
  댓글 수: 2
Jen B
Jen B 2022년 2월 12일
Thank you!
William Rose
William Rose 2022년 2월 13일
You're welcome @Jen B

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

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 2월 12일
I wonder if gscatter does what you want. There is no line, however.
time = ['15:02:03'; '15:02:03'; '15:02:04'; '15:02:04'; '15:02:05'; '15:02:05'; '15:02:06'; '15:02:06'; '15:02:07'; '15:02:07'];
ID = [1 2 1 2 1 2 1 2 1 2]';
range = [24.492 25.653 24.489 25.65 24.485 25.646 24.482 25.643 24.478 25.639]';
data = table(time,ID,range);
% convert times to duration
data.time = duration(string(data.time),"InputFormat","hh:mm:ss")
data = 10×3 table
time ID range ________ __ ______ 15:02:03 1 24.492 15:02:03 2 25.653 15:02:04 1 24.489 15:02:04 2 25.65 15:02:05 1 24.485 15:02:05 2 25.646 15:02:06 1 24.482 15:02:06 2 25.643 15:02:07 1 24.478 15:02:07 2 25.639
gscatter(data.time, data.range,data.ID)
If you want a line, perhaps unstack can be used to get you closer to what you want.
idData = unstack(data,"range","ID")
Warning: Table variable names that were not valid MATLAB identifiers have been modified. Since table variable names must be unique, any table variable names that happened to match the new identifiers also have been modified.
To use the original INDVAR values as table variable names, set 'VariableNamingRule' to 'preserve'.
idData = 5×3 table
time x1 x2 ________ ______ ______ 15:02:03 24.492 25.653 15:02:04 24.489 25.65 15:02:05 24.485 25.646 15:02:06 24.482 25.643 15:02:07 24.478 25.639
plot(idData.time,idData{:,["x1","x2"]})
legend

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by