making import function for files that have different datetime format
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)?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)? 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)? datetime, import, function, multiple MATLAB Answers — New Questions