필터 지우기
필터 지우기

Importing month into Matlab is not recognized

조회 수: 1 (최근 30일)
Sander Bovee
Sander Bovee 2019년 10월 30일
댓글: Peter Perkins 2019년 10월 30일
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.

채택된 답변

Roofus Milton
Roofus Milton 2019년 10월 30일
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
Sander Bovee
Sander Bovee 2019년 10월 30일
Thanks !
Peter Perkins
Peter Perkins 2019년 10월 30일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by