Plot data vs time color coded by ID
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jen B
el 12 de Feb. de 2022
Comentada: William Rose
el 13 de Feb. de 2022
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 comentarios
Image Analyst
el 12 de Feb. de 2022
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')
Respuesta aceptada
William Rose
el 12 de Feb. de 2022
Editada: William Rose
el 12 de Feb. de 2022
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 comentarios
Más respuestas (1)
Cris LaPierre
el 12 de Feb. de 2022
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")
gscatter(data.time, data.range,data.ID)
idData = unstack(data,"range","ID")
plot(idData.time,idData{:,["x1","x2"]})
legend
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


