making import function for files that have different datetime format
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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)?
1 Kommentar
Antworten (1)
Star Strider
am 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'}
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)))
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.
.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!