Error using datenum (line 201) DATENUM failed.

Help me!
I have this error, and I don't know why.
Error using datenum (line 201)
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed to convert from text to date number.
My script is this:
[ndata, text, alldata]=xlsread('positive_read_Copia.xlsx'); % inside there are data (dd/mm/yyyy hh:mm:ss) and coordinates
date=date.';
tmp=char(date);
tmp_date=datenum(tmp,'dd/mm/yyyy HH:MM');
DateVector = datevec(tmp_date);
Date_minute=(tmp_date-tmp_date(1))*24*60; % Time, in minutes compared to the 1st georeferenced tweet
clear tmp;

2 Comments

Please share your spreadsheet. You can attach it using the paperclip icon.

Sign in to comment.

Answers (1)

Do not use outdated, discouraged, deprecated DATENUM (or for that matter DATEVEC or DATESTR).
Import your data using READTABLE, which automatically identifies the dates and imports them as DATETIME objects:
T = readtable('positive_read_Copia.xlsx')
T = 1108×3 table
tweet_created_at Latitude Longitude ____________________ ________ _________ 28-Sep-2020 01:13:00 4.8186 45.728 28-Sep-2020 02:11:00 -80.217 25.783 28-Sep-2020 03:20:00 4.4894 45.895 28-Sep-2020 08:38:00 -74.245 40.617 28-Sep-2020 09:32:00 4.0817 44.128 28-Sep-2020 09:46:00 4.0817 44.128 28-Sep-2020 10:20:00 103.45 35.845 28-Sep-2020 10:22:00 4.0817 44.128 28-Sep-2020 10:41:00 2.5067 49.098 28-Sep-2020 11:55:00 6.8992 49.53 28-Sep-2020 12:34:00 15.314 -4.3317 28-Sep-2020 12:50:00 35.25 32 28-Sep-2020 13:46:00 -14.75 16.5 28-Sep-2020 14:05:00 2 47 28-Sep-2020 14:36:00 7.4167 43.733 28-Sep-2020 16:24:00 9.2167 45.617
Then your task is much easier too, simply subtract to get the differences as DURATION objects:
D = T.tweet_created_at - T.tweet_created_at(1)
D = 1108×1 duration array
00:00:00 00:58:00 02:07:00 07:25:00 08:19:00 08:33:00 09:07:00 09:09:00 09:28:00 10:42:00 11:21:00 11:37:00 12:33:00 12:52:00 13:23:00 15:11:00 15:24:00 15:42:00 16:55:00 18:10:00 18:15:00 21:16:00 21:26:00 22:42:00 30:23:00 31:14:00 32:11:00 32:23:00 37:02:00 37:55:00
which is then trivial to convert into MINUTES:
M = minutes(D)
M = 1108×1
0 58 127 445 499 513 547 549 568 642

4 Comments

but doing this I have another error :(
One moment, this is the script. In my opinion there is one problem with format of the datatime.
I don't know.
%% Loading dataset
% Loading geolocalized tweets
[ndata, text, alldata]=xlsread('positive_read_Copia.xlsx');
% Loading GIS layer for the map
S = shaperead('Countries.shp');
for(i=1:(size(text,1)-1))
date(i)=text(i+1,1);
end
X(:,1)=ndata(:,2); % longitude
X(:,2)=ndata(:,1); % latitude
date=date.';
tmp=char(date);
tmp_date=datenum(tmp,'dd/mm/yyyy HH:MM');
DateVector = datevec(tmp_date);
Date_minute=(tmp_date-tmp_date(1))*24*60; % Time, in minutes compared to the 1st georeferenced tweet
clear tmp;
%% Run DBSCAN Clustering Algorithm
epsilon_time=60; % Maximum duration (in minutes) to declare that 2 points are temporally correlated
epsilon_dist=30; % Maximum distance (in km) to declare that 2 points are spatially correlated
MinPts=20; % Minimun number of correlated points to declare a new cluster
IDX=DBSCAN_time(X,Date_minute,epsilon_dist,epsilon_time,MinPts);
%% Dimension reduction (ie. groupings duplicate points having the same geographic coordinates)
tmp_IDX=X;
tmp_IDX(:,3)=IDX(:);
[C,ia,ic] = unique(tmp_IDX(:,1:3),'rows');
a_counts = accumarray(ic,1);
IDX_points2point = [C, a_counts];
%% Plot Results
subplot(3,1,[1,2])
mapshow(S);
hold on
Legends=PlotClusterinResult_points2point_floods(X, IDX_points2point);
Legends_histo = {};
title(['DBSCAN Clustering (\epsilon distance = ' num2str(epsilon_dist) ' km, \epsilon time = ' num2str(epsilon_time) ' minutes, MinPts = ' num2str(MinPts) ')']);xlim([-6 10])
ylim([41 52])
k=max(IDX);
Colors=hsv(k);
subplot(3,1,[1,2])
if k>=0
for(i=1:k)
tmp_cluster=find(IDX==i);
tmp_bd = [X(tmp_cluster(:),1) X(tmp_cluster(:),2)];
bound=boundary(tmp_bd(:,1),tmp_bd(:,2));
tmp_histo=tmp_date(tmp_cluster(:));
stat_clust(i,1)=mean(tmp_histo*24*60)/(24*60);
stat_clust(i,2)=std(tmp_histo);
Color = Colors(i,:);
subplot(3,1,[1,2])
hold on
plot(tmp_bd(bound,1),tmp_bd(bound,2),'Color',Color);
Legends{end+1} = ['Area cluster n°' num2str(i)];
subplot(3,1,3)
hold on
grid on
h=histogram(tmp_histo);
h.FaceColor = Color;
h.BinWidth = 8/24;
Legends_histo{end+1} = ['Cluster n°' num2str(i),' Tmean=' datestr((stat_clust(i,1)),'dd-mm HH'),'+/-' datestr((stat_clust(i,2)),'HH'),' hours'];
clear tmp_cluster tmp_bd bound tmp_histo
end
end
subplot(3,1,3)
legend(Legends_histo);
legend('Location', 'NorthEastOutside');
xlabel('Date')
ylabel('Distribution')
axis([datenum('2020-09-30') datenum('2020-10-07') 0 200])
datetick('x','dd-mm HH')
Stephen23
Stephen23 on 18 Nov 2021
Edited: Stephen23 on 18 Nov 2021
The code that you show in your comment is completly unrelated to my answer, so it is not clear what you expect from me. In any case, you should replace those deprecated functions with DATETIME and DURATION objects.
"but doing this I have another error"
Please show the complete error message. This means all of the red text.
Error is: I have the map and the graph empty.

Sign in to comment.

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!