making import function for files that have different datetime format

1 view (last 30 days)
Hi,
I am having some difficulties in importing a file that has datetime format. The main problem is that sometimes the file I want to import has millisecond information and sometimes it does not.
Below is the function that I use to import the file.
function T_data = func_import_MCC_v2(filename, dataLines)
% If dataLines is not specified, define defaults
if nargin < 2
dataLines = [8, Inf];
end
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 4, "Encoding", "UTF-8");
% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["num", "timestamp", "ch1", "ch2"];
opts.VariableTypes = ["double", "datetime", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, "timestamp", "InputFormat", "MM/dd/yyyy hh:mm:ss.SSS aa");
% Import the data
T_data = readtable(filename, opts);
%%
T_data.timestamp.TimeZone = 'America/Denver';
T_data.timestamp.Format = "dd-MMM-uuuu HH:mm:ss";
Sometimes I need to change one of the code lines as follows (in case the data does not have millisecond information)
opts = setvaropts(opts, "VarName2", "InputFormat", "MM/dd/yyyy hh:mm:ss aa");
Is there any way that I can use a single function that is compatible with the cases (whether it has millisecond information or not)?

Answers (1)

Star Strider
Star Strider on 27 Sep 2024
I am not certain how you could use this in your import options call, however one approach could be this —
DC = {'9/27/2024 8:53:10 AM'; '9/27/2024 8:53:10.123 AM'; '9/28/2024 12:53:10.456 AM'; '9/29/2024 1:53:10 PM'}
DC = 4x1 cell array
{'9/27/2024 8:53:10 AM' } {'9/27/2024 8:53:10.123 AM' } {'9/28/2024 12:53:10.456 AM'} {'9/29/2024 1:53:10 PM' }
DTfcn = @(dates) cat(1, datetime(dates, "InputFormat", "MM/dd/yyyy hh:mm:ss.SSS aa", "Format","MM/dd/yyyy hh:mm:ss.SSS aa"), datetime(dates, "InputFormat", "MM/dd/yyyy hh:mm:ss aa", "Format","MM/dd/yyyy hh:mm:ss.SSS aa"));
DTv = DTfcn(DC);
DTv = sort(DTv(~isnat(DTv)))
DTv = 4x1 datetime array
09/27/2024 08:53:10.000 AM 09/27/2024 08:53:10.123 AM 09/28/2024 12:53:10.456 AM 09/29/2024 01:53:10.000 PM
That would involve importing them as character vectors or string arrays, then converting them to datetime arrays. I cannot guarantee that it would also preserve the order of the inplut argument (‘DC’ here), so it would likely be necessary to sort them, as in this example, to be sure.
.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!