Convert datetime to datestr without losing milisecond data
I am calling in time and date data from an excel spreadsheet using the readcell function, with the resultant column consisting of datetime arrays (column 4). I tried to set the variable type as ‘string’ to solve the below problem, but it is still calling in collumn 4 as datetime.
opts = detectImportOptions(fname);
opts.Sheet = 2;
opts = setvartype(opts, ‘string’);
Data = readcell(fname,opts);
The problem I am having is that the original data called in has milliseconds included:
>> Data{1,4}
ans =
datetime
22-Jul-2023 15:18:15.171
But when I convert the datetime data into a string:
datestr(ans)
the result is
’22-Jul-2023 15:18:15′
losing the millisecond information.
Basically, I want to search the next line to see if the next datetime occurs within 2 seconds of the original (ie. if Data{2,4} is within 2.00 seconds of Data{1,4}) without losing the milisecond info.
So either is there a different way of converting the datetime to a string or char or anything that will not lose the millisecond information?
Or is there another way of calling in the spreadsheet data in the first place?
The data I want to call in from the spreadsheet is:
collumn 1: file names (includes numbers and letters)
collumn 2: participant name (letters)
collumn 4: datetime
collumn 7: data (numbers)
Thanks in advance!I am calling in time and date data from an excel spreadsheet using the readcell function, with the resultant column consisting of datetime arrays (column 4). I tried to set the variable type as ‘string’ to solve the below problem, but it is still calling in collumn 4 as datetime.
opts = detectImportOptions(fname);
opts.Sheet = 2;
opts = setvartype(opts, ‘string’);
Data = readcell(fname,opts);
The problem I am having is that the original data called in has milliseconds included:
>> Data{1,4}
ans =
datetime
22-Jul-2023 15:18:15.171
But when I convert the datetime data into a string:
datestr(ans)
the result is
’22-Jul-2023 15:18:15′
losing the millisecond information.
Basically, I want to search the next line to see if the next datetime occurs within 2 seconds of the original (ie. if Data{2,4} is within 2.00 seconds of Data{1,4}) without losing the milisecond info.
So either is there a different way of converting the datetime to a string or char or anything that will not lose the millisecond information?
Or is there another way of calling in the spreadsheet data in the first place?
The data I want to call in from the spreadsheet is:
collumn 1: file names (includes numbers and letters)
collumn 2: participant name (letters)
collumn 4: datetime
collumn 7: data (numbers)
Thanks in advance! I am calling in time and date data from an excel spreadsheet using the readcell function, with the resultant column consisting of datetime arrays (column 4). I tried to set the variable type as ‘string’ to solve the below problem, but it is still calling in collumn 4 as datetime.
opts = detectImportOptions(fname);
opts.Sheet = 2;
opts = setvartype(opts, ‘string’);
Data = readcell(fname,opts);
The problem I am having is that the original data called in has milliseconds included:
>> Data{1,4}
ans =
datetime
22-Jul-2023 15:18:15.171
But when I convert the datetime data into a string:
datestr(ans)
the result is
’22-Jul-2023 15:18:15′
losing the millisecond information.
Basically, I want to search the next line to see if the next datetime occurs within 2 seconds of the original (ie. if Data{2,4} is within 2.00 seconds of Data{1,4}) without losing the milisecond info.
So either is there a different way of converting the datetime to a string or char or anything that will not lose the millisecond information?
Or is there another way of calling in the spreadsheet data in the first place?
The data I want to call in from the spreadsheet is:
collumn 1: file names (includes numbers and letters)
collumn 2: participant name (letters)
collumn 4: datetime
collumn 7: data (numbers)
Thanks in advance! datetime, strings, milliseconds, readcell MATLAB Answers — New Questions