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)
plot(DayGroups{k}{:,2}, DayGroups{k}{:,3:end}, 'bp', DayGroups_t {k}{:,2}, DayGroups_t{k}{:,3:end}, 'r:')
That ends up plotting things from Sep-27 with things from Sep-30 and so on. Any ideas on how to fix this?

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'
lat_avistagens = [-24.13043
lon_avistagens = [-41.59167
data_tracking_tratado = datetime({'30-Sep-2019'
lat_tracking_tratado = [-24.285783
lon_tracking_tratado = [-41.74071
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);
hold on
hold off
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).
grid on
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?


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

Sign in to comment.

More Answers (0)

Sign in to answer this question.