MATLAB Answers

Help plotting two events that happened on the same day

2 views (last 30 days)
Hello everyone,
I am currently with this code right here:
plot_avistagens = table(data_avistagens,lat_avistagens,lon_avistagens);
plot_tracking = table(data_tracking_tratado,lat_tracking_tratado,lon_tracking_tratado);
here is how plot_avistagens is organised (first column is datetime)
'27-Sep-2019' -24,1304300000000 -41,5916700000000
'27-Sep-2019' -24,1280000000000 -41,5905200000000
'29-Sep-2019' -25,2749500000000 -41,1169800000000
'30-Sep-2019' -25,3730300000000 -41,3620800000000
'30-Sep-2019' -25,3766300000000 -41,4046500000000
'30-Sep-2019' -25,3780300000000 -41,4305200000000
'30-Sep-2019' -25,3780300000000 -41,4305200000000
'30-Sep-2019' -25,2720800000000 -41,6465300000000
'30-Sep-2019' -25,1177000000000 -41,8814000000000
'01-Oct-2019' -24,4056700000000 -41,8560000000000
'01-Oct-2019' -24,4056700000000 -41,8560000000000
'09-Oct-2019' -24,6301200000000 -40,6946300000000
and here is plot_tracking (first column also datetime)
'30-Sep-2019' -24,2857830000000 -41,7407100000000
'30-Sep-2019' -24,2598050000000 -41,7166830000000
'30-Sep-2019' -24,2284610000000 -41,6914590000000
'30-Sep-2019' -24,1944140000000 -41,6667960000000
'30-Sep-2019' -24,1683580000000 -41,6296230000000
'30-Sep-2019' -24,1460220000000 -41,5832420000000
'01-Oct-2019' -24,1260030000000 -41,5351400000000
'01-Oct-2019' -24,1162640000000 -41,4840100000000
'01-Oct-2019' -24,1061480000000 -41,4380680000000
'01-Oct-2019' -24,0954680000000 -41,3900660000000
'01-Oct-2019' -24,0850860000000 -41,3420080000000
Now my next step is plotting different charts for each day. The difficulty I'm facing is: I need a chart that shows all the events that happened on each day. However, my code just plots "DayGroups day 1" with "DayGroups_t day 1" and doesnt check if "DayGroups day 1" is the same day as "DayGroups_t day 1"
D = findgroups(datetime(plot_avistagens{:,1}));
DayGroups = accumarray(D, (1:size(D,1))', [], @(x){plot_avistagens(x,:)});
Dt = findgroups(datetime(plot_tracking{:,1}));
DayGroups_t = accumarray(Dt, (1:size(Dt,1))', [], @(x){plot_tracking(x,:)});
for k = 1:size(DayGroups_t,1)
figure(k)
plot(DayGroups{k}{:,2}, DayGroups{k}{:,3:end}, 'bp', DayGroups_t {k}{:,2}, DayGroups_t{k}{:,3:end}, 'r:')
grid
end
That ends up plotting things from Sep-27 with things from Sep-30 and so on. Any ideas on how to fix this?
Thanks!

Accepted Answer

Cris LaPierre
Cris LaPierre on 28 Oct 2019
I'm not sure what it is you want to display on your final figure.
First, for others, here's the data in a way you can quickly create the variables used.
data_avistagens = datetime({'27-Sep-2019'
'27-Sep-2019'
'29-Sep-2019'
'30-Sep-2019'
'30-Sep-2019'
'30-Sep-2019'
'30-Sep-2019'
'30-Sep-2019'
'30-Sep-2019'
'01-Oct-2019'
'01-Oct-2019'
'09-Oct-2019'});
lat_avistagens = [-24.13043
-24.128
-25.27495
-25.37303
-25.37663
-25.37803
-25.37803
-25.27208
-25.1177
-24.40567
-24.40567
-24.63012];
lon_avistagens = [-41.59167
-41.59052
-41.11698
-41.36208
-41.40465
-41.43052
-41.43052
-41.64653
-41.8814
-41.856
-41.856
-40.69463];
data_tracking_tratado = datetime({'30-Sep-2019'
'30-Sep-2019'
'30-Sep-2019'
'30-Sep-2019'
'30-Sep-2019'
'30-Sep-2019'
'01-Oct-2019'
'01-Oct-2019'
'01-Oct-2019'
'01-Oct-2019'
'01-Oct-2019'});
lat_tracking_tratado = [-24.285783
-24.259805
-24.228461
-24.194414
-24.168358
-24.146022
-24.126003
-24.116264
-24.106148
-24.095468
-24.085086];
lon_tracking_tratado = [-41.74071
-41.716683
-41.691459
-41.666796
-41.629623
-41.583242
-41.53514
-41.48401
-41.438068
-41.390066
-41.342008];
plot_avistagens = table(data_avistagens,lat_avistagens,lon_avistagens);
plot_tracking = table(data_tracking_tratado,lat_tracking_tratado,lon_tracking_tratado);
It looks like you are trying to create a plot for each day that just displays the events that occurred on that day?
Using the variables I created, I would do it like this.
Ds = categorical([data_avistagens;data_tracking_tratado]);
D = categories(Ds);
for d = 1:length(D)
idxD = data_avistagens==D(d);
idxDt = data_tracking_tratado==D(d);
figure
plot(plot_avistagens.lat_avistagens(idxD),plot_avistagens.lon_avistagens(idxD),'bp');
hold on
plot(plot_tracking.lat_tracking_tratado(idxDt),plot_tracking.lat_tracking_tratado(idxDt),'ro');
hold off
xlabel("Lat")
ylabel("Lon")
title(D(d))
end
You might want to looking into using gscatter to create a group scatter plot using the date as the grouping variable (I've converted it to a categorical so I could do this).
figure
gscatter(plot_avistagens.lat_avistagens,plot_avistagens.lon_avistagens,plot_avistagens.data_avistagens)
grid on
figure
gscatter(plot_tracking.lat_tracking_tratado,plot_tracking.lon_tracking_tratado,plot_tracking.data_tracking_tratado)
grid on
This is creating 2 separate figures.
To be honest, I couldn't understand what exactly it was you wanted to display. Perhaps this is enough to help you get started?

  3 Comments

Arthur Romeu
Arthur Romeu on 29 Oct 2019
Haha thanks!
I'll briefly explain what I'm trying to do. Maybe it will make more sense.
During an oceanographic expedition, the ship recorded its position on an hourly basis (data_tracking, lat_tracking and lon_tracking). During those days of expedition, marine mammals (whales and dolphins) were registered on another table (data_avistagens, lat_avistagens, lon_avistagens). To quickly confirm that the coordinates of the "encounters" are correct (if it's too far from the tracking data there is no way someone could've seen the animal) I am trying to display the tracking data from a given day along with the whale watching data from that day.
In other words, I'm trying to visually identify if all of the whales and dolphins have infact the right coordinates by plotting the events of the day along with the tracking of the day on an X Y chart.
Did it make sense? hahaha I'm really new to programming on matlab. Thanks again for your patience.
Cris LaPierre
Cris LaPierre on 31 Oct 2019
The simplest way I've found is the following:
figure
gscatter([plot_avistagens.lat_avistagens;plot_tracking.lat_tracking_tratado],[plot_avistagens.lon_avistagens; plot_tracking.lon_tracking_tratado],categorical([plot_avistagens.data_avistagens;plot_tracking.data_tracking_tratado]))
grid on
There is probably a better way to join the data, but here you can see what is happening.
All data will have the same symbol, but it should be fairly obvious where the boat is vs where the marine mammals are.
ArthruRomeu_plot.png

Sign in to comment.

More Answers (0)

Sign in to answer this question.