making import function for files that have different datetime format
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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)?
Risposte (1)
Star Strider
il 27 Set 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 Commenti
Vedere anche
Categorie
Scopri di più su Data Import and Analysis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!