Importing month into Matlab is not recognized

1 view (last 30 days)
Goodday,
I'm trying to import a txt/CSV file into matlab by using datastore. (Also see attached txt file)
NO.,Time,ms,"V","V","Hz","Hz","A1234567890","ALP1-4","AO1234"
1,2019/10/10 11:09:15, 0,+ 1.883,+ 24.00, 78, 37,LLLLLLLLLL,LLLL,LLLL
2,2019/10/10 11:09:15,200,+ 1.886,+ 24.00, 78, 37,LLLLLLLLLL,LLLL,LLLL
So in order to import with the correct format, my script is:
dat = datastore('TEST.txt')
dat.SelectedVariableNames = dat.SelectedVariableNames([1:7]);
dat.SelectedFormats{1} = '%f';
dat.SelectedFormats{2} = '%{YYYY/MM/DD HH:mm:ss}D';
dat.SelectedFormats{3} = '%f';
dat.SelectedFormats{4} = '+%f';
dat.SelectedFormats{5} = '+%f';
dat.SelectedFormats{6} = '%f';
dat.SelectedFormats{7} = '%f';
However when i run a preview (or import) the data i get the following result:
Number Date_Time ms CH1 CH2 Pulse1 Pulse2
______ ___________________ ___ _____ ___ ______ ______
1 2019/01/10 11:09:15 0 1.883 24 78 37
2 2019/01/10 11:09:15 200 1.886 24 78 37
3 2019/01/10 11:09:15 400 1.883 24 78 36
4 2019/01/10 11:09:15 600 1.884 24 79 37
5 2019/01/10 11:09:15 800 1.886 24 78 37
6 2019/01/10 11:09:16 0 1.883 24 78 36
7 2019/01/10 11:09:16 200 1.882 24 79 37
The month has been set to januari instead of oktober and i don't understand why matlab does not reconizing the month from the original data correctly.
Any help would be much appreciated.

Accepted Answer

Roofus Milton
Roofus Milton on 30 Oct 2019
Sander-
Simply change the capitalizations of your date format.
% location of data file
filePath = "TEST.txt";
% variables from sample code and file
variableNames = ["Number", "Date_Time", "ms", "CH1", "CH2", "Pulse1", "Pulse2", "Var8", "Var9", "Var10"];
% variable format strings
variableFormats = ["%f", "%{yyyy/MM/dd HH:mm:ss}D", "%f", "+%f", "+%f", "%f", "%f", "%s", "%s", "%s"];
% initialize the datastore object
dat = datastore(filePath, ...
"VariableNames", variableNames, ...
"TextscanFormats", variableFormats, ...
"TextType", "string");
% store sample data
sampleData = preview(dat);
% test date parts
dateParts = [year(sampleData.Date_Time), month(sampleData.Date_Time), day(sampleData.Date_Time), ...
hour(sampleData.Date_Time), minute(sampleData.Date_Time), second(sampleData.Date_Time)]
  2 Comments
Peter Perkins
Peter Perkins on 30 Oct 2019
Just to be clear: DD is day of year. It occurs after the MM in your format, and so month is read as 10, but then day of year is read as 10 and overwrites the month value.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!