MATLAB: How to solve ‘SamplePoints’ value contains Inf or NaT.

datetimeinfMATLABmoving averagenat;samplepoints

Hi everyone.
Can anyone help me to solve this error? The error showing this:
Error using convertMovfunArgs (line 19)
'SamplePoints' value contains Inf or NaT.
Error in datetime/movmean (line 104)
args = convertMovfunArgs(varargin{:});
Error in MAFilt_loop (line 33)
M{i} = movmean(y,k,'SamplePoints',dtm);
I try to do a loop in my script as shown below since I have many files to perform a moving average function. Attached also the example of my input files.I hope that someone can help me solve this matter. Thank you.
*Note: the input files should be kept in MtxjP1 folder and execute the script outside of MtxjP1 folder.
clc;clear all;close all;
p = input('Please enter the pass number:');
for pid=p
%% EDIT FILENAME HERE
path = sprintf('MtxjP%d',pid);
BasePath = 'F:\ERM\Merge_txj\3_Thres_MovAvg\';
newPath = [BasePath path];
txtfiles = dir([newPath '*.txt']);
% Get a list of all files, including folders.
DirList = dir(newPath);
% Get rid of first two folders: dot and dot dot.
DirList = DirList(3:end);
% Sort the files in ascending numbers
[~,ndx] = natsortfiles({DirList.name});
filenames = DirList(ndx);
numfiles = length(filenames);
B=1;
for i= 1:numfiles
%% EDIT FILENAME HERE
formatSpec = sprintf('Mtxjp_FPT%d.txt',i);
A{i} = dlmread(sprintf([path '/' formatSpec],B));
B=B+1;
y = A{i}(:,4);
k = days(90);
dt = num2str(A{i}(:,1),'%.2f');
dtm = datetime(dt,'InputFormat','yyyyMMddHHmmss.SS');
% end

% end
M{i} = movmean(y,k,'SamplePoints',dtm);
Fdata = [A{i}(:,1:3) M{i} A{i}(:,5:7)]
filename = sprintf('Mtxjp_MvPT%d.txt', i);
fid = fopen([path '/' filename],'wt');
%fprintf(fid,'%.12f \t %.6f \t %.6f \t %.3f \t %.0f \t %.0f \t %.4f\n',Fdata.');
fprintf(fid,'%17.2f \t %.6f \t %.6f \t %.3f \t %.0f \t %.0f \t %.4f\n',Fdata.');
fclose(fid);
end
end

Best Answer

A relevant section of your code:
dtm = datetime(dt,'InputFormat','yyyyMMddHHmmss.SS');
% end

% end
M{i} = movmean(y,k,'SamplePoints',dtm);
My guess is that some of the entries in dt don't match the InputFormat you specified and so the corresponding elements of dtm are NaT. Here's a smaller example that demonstrates what I mean.
>> q = datetime({'2020-03-05'; '2020'}, 'InputFormat', 'yyyy')
q =
2×1 datetime array
NaT
01-Jan-2020
'2020-03-05' doesn't match the format 'yyyy'. Use isnat to locate the NaT elements in dtm and check the corresponding entries in dt. If they all have a second format convert them using that second format and use logical indexing with the output of isnat to replace the NaT values in dtm.