MATLAB Answers

How to plot data from a single day?

8 views (last 30 days)
Hello everyone! I am currently a novice on the matlab world.
I have a table that goes as such:
"10-Jul-2019 -24.129 -40.848
10-Jul-2019 -24.129 -40.818
10-Jul-2019 -24.132 -40.512
10-Jul-2019 -24.132 -40.393
13-Jul-2019 -24.578 -40.304
14-Jul-2019 -24.675 -41.144
14-Jul-2019 -24.573 -40.959
14-Jul-2019 -24.573 -40.798
14-Jul-2019 -24.573 -40.798
14-Jul-2019 -24.574 -40.679
14-Jul-2019 -24.573 -40.569
14-Jul-2019 -24.574 -40.507
15-Jul-2019 -24.682 -40.93
17-Jul-2019 -24.761 -41.586 "
And I want to plot a different chart for each day.
Does anyone know how to do this?
Thanks!!

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 25 Oct 2019
If ‘T1’ is your table, and y0ou defined it with a datetime array as the first column try this:
G = findgroups(day(T1{:,1}));
DayGroups = accumarray(G, (1:size(G,1))', [], @(x){T1(x,:)});
Then see: Plot Dates and Durations to do the plotting.
Example —
for k = 1:size(DayGroups,1)
figure(k)
plot(DayGroups{k}{:,1}, DayGroups{k}{:,2:end}, '-p')
grid
end
This worked with data that I already have in a similar table that I have already defined. I have no idea what your table actually is, or how you have defined it, so I cannot use the text that you posted.

  6 Comments

Show 3 older comments
Star Strider
Star Strider on 28 Oct 2019
As always, my pleasure!
My code was predicated on the time variable already being a datetime object.
Example —
C = {'10-Jul-2019' -24.132 -40.512
'10-Jul-2019' -24.132 -40.393
'13-Jul-2019' -24.578 -40.304
'14-Jul-2019' -24.675 -41.144
'14-Jul-2019' -24.573 -40.959};
D1 = table(datetime(C(:,1)), 'VariableNames',{'Date'}); % Create ‘datetime’ Table
D2 = cell2table(C(:,2:3)); % Table For Other Variables
T1 = [D1,D2]; % Concatenate To Create ‘T1’
G = findgroups(day(T1{:,1}));
DayGroups = accumarray(G, (1:size(G,1))', [], @(x){T1(x,:)});
I was initially not certain how you had imported or defined the first column, so I was hesitant to provide specific code to create the table.
Arthur Romeu
Arthur Romeu on 30 Oct 2019
I am really struggling to make the images right :(
Your code is awesome and I never thought I could group my data on this manner. I didn't know I could keep asking here so that's why I created the other topic.
I'm having a hard time figuring out how to make the charts plot the way I want to. Right now I have this piece of code:
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
Both cell groups "DayGroups" and "DayGroups_t" have the same structure (datetime,Xcoordinates,Ycoordinates). However, what I need is to plot the events that happened on the same day from both cell groups. Right now what the code is doing is plotting the first day of DayGroups with the first day of DayGroups_t, but they are not the same day. I will include the theory behing what I'm trying to do and both DayGroups.mat and DayGroups_t.mat for better understanding.
"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."
This is the full code:
%% Preallocate Memory
lat_avistagens = [];
lon_avistagens = [];
ano_avistagens = [];
mes_avistagens = [];
dia_avistagens = [];
data_avistagens = [];
plot_avistagens = [];
D = [];
Dt = [];
DayGroups = [];
DayGroups_t = [];
lat_tracking = [];
lon_tracking = [];
ano_tracking = [];
mes_tracking = [];
dia_tracking = [];
data_tracking = [];
plot_tracking = [];
quit = [];
data_tracking_tratado = [];
lat_tracking_tratado = [];
lon_tracking_tratado = [];
%% Parte 1: avistagens
% Caminho para o RGA
path_ = 'c:\Users\ivan_\Desktop\tmp_arthur\matlabb\table1.xlsx';
%% Trabalhando com a matriz
ImportOptions_ = detectImportOptions(path_, 'NumHeaderLines', 1, 'Sheet', 3);
aux1 = readtable(path_,ImportOptions_);
lat = table2array(aux1(:,5));
lon = table2array(aux1(:,6));
ano = table2array(aux1(:,2));
mes = table2array(aux1(:,3));
dia = table2array(aux1(:,4));
lat_avistagens=[lat_avistagens;lat];
lon_avistagens=[lon_avistagens;lon];
ano_avistagens=[ano_avistagens;ano];
mes_avistagens=[mes_avistagens;mes];
dia_avistagens=[dia_avistagens;dia];
err = 'Latitude apresenta inconformidade.';
erro = 'Longitude apresenta inconformidade.';
if lat_avistagens < -30 %esses valores são arbitrários
error(err);
end
if lon_avistagens < -45 %esses valores são arbitrários
error(erro);
end
%save dados_avistagens.mat
clearvars lat lon ano mes dia aux1 ImportOptions_ path_
%% Organizando datas
data_avistagens = datetime(ano_avistagens,mes_avistagens,dia_avistagens,'TimeZone','America/Sao_Paulo');
%data_avistagens.TimeZone = 'America/Sao_Paulo'; nesse caso não precisa
%disso porque as datas originais já estão no horário de Brasília.
%% Parte 2: tracking
% Caminho para a planilha de Tracking
path_ = 'c:\Users\ivan_\Desktop\tmp_arthur\matlabb\table2.xlsx';
%% Trabalhando com a matriz
% Necessario laco para ler varias abas (sheets) - repeticao de acoes
%for cgg = 1:36
ImportOptions_ = detectImportOptions(path_, 'NumHeaderLines', 6);
aux1 = readtable(path_,ImportOptions_);
lat = table2array(aux1(:,10));
lon = table2array(aux1(:,9));
ano = table2array(aux1(:,2));
mes = table2array(aux1(:,3));
dia = table2array(aux1(:,4));
lat_tracking=[lat_tracking;lat];
lon_tracking=[lon_tracking;lon];
ano_tracking=[ano_tracking;ano];
mes_tracking=[mes_tracking;mes];
dia_tracking=[dia_tracking;dia];
clearvars lat lon ano mes dia aux1 ImportOptions_ path_
%% Organizando datas
data_tracking = datetime(ano_tracking,mes_tracking,dia_tracking,'TimeZone','UTC');
data_tracking.TimeZone = 'America/Sao_Paulo';
%% Reduzindo a resolucao temporal: quit = nº de celulas a pular
quit = 59 ;
data_tracking_tratado = data_tracking(1:quit:end) ;
lat_tracking_tratado = lat_tracking(1:quit:end) ;
lon_tracking_tratado = lon_tracking(1:quit:end) ;
%% Plotagem dos gráficos
plot_avistagens = table(data_avistagens,lat_avistagens,lon_avistagens);
plot_tracking = table(data_tracking_tratado,lat_tracking_tratado,lon_tracking_tratado);
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
I really need some help :,( I tried using functions find() and datefind() but I can't make this work
sorry to keep bothering with this
Arthur Romeu
Arthur Romeu on 30 Oct 2019
Hi! is this a good way to think?
for k = 1:size(DayGroups_t,1)
j = 1:size(DayGroups,1);
if DayGroups_t{j}{1,1} == DayGroups{k}{1,1}
figure(k)
plot(DayGroups{k}{:,2}, DayGroups{k}{:,3:end}, 'bp', DayGroups_t {k}{:,2}, DayGroups_t{k}{:,3:end}, 'r:')
grid
else
j = j+1
end
end
It says that "Index exceeds matrix dimensions." What am I doing wrong?

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags