필터 지우기
필터 지우기

.csv Date and Time assigned Data Import

조회 수: 1 (최근 30일)
Georg Söllinger
Georg Söllinger 2016년 11월 4일
댓글: Georg Söllinger 2016년 11월 7일
Hi dear Community,
I need to import csv data from Trials into MATLAB. The dataset is as follows:
2016/11/02 17:51:54,017;256,451812744141;0;0;0,130208298563957
I tried to achieve the import with csvread, but I guess, matlab cannot handle date and time in one cell?! Another problem may be, that the decimal delimiter symbol is ',' and not '.'. Actually, I don't need the date, I only need the time and the difference of the time between adjacent measurements as an additional column.
Help is very appreciated, thanks therefore in advance! Best Wishes, Georg

채택된 답변

Jeremy Hughes
Jeremy Hughes 2016년 11월 7일
Hi George,
If you're using R2016b you can import this directly, but it will take a little bit of configuration The following should work:
opts = detectImportOptions(yourfilename,'Delimiter',';')
opts.VariableTypes = {'datetime','double','double','double','double'};
opts = setvaropts(opts,1,'InputFormat','yyyy/MM/DD HH:mm:ss,SSS');
opts = setvaropts(opts,2:5,'DecimalSeparator',',');
T = readtable(yourfilename,opts)
  댓글 수: 1
Georg Söllinger
Georg Söllinger 2016년 11월 7일
Oh perfect, this works very fine!! In the meantime, I tried to achieve a solution with the editor and excel, but this solution is definitely more elegant!
Thank you all for your answers.

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

추가 답변 (1개)

dbmn
dbmn 2016년 11월 4일
Hi Georg,
if you already extracted the required string, it can easily be converted into a datetime variable using a custom inputformat (that is also capable of dealing with commas)
mytimevar = datetime('2016/11/02 17:51:54,017', 'InputFormat', 'yyyy/MM/DD HH:mm:ss,SSS');
if you still need to extract the string I may suggest stringsplit
  댓글 수: 2
Georg Söllinger
Georg Söllinger 2016년 11월 4일
편집: Georg Söllinger 2016년 11월 4일
Thanks for your answer dbmn, but it is only half the answer.
Well, as said, matlab throws an error if I make the csv import.
The code is as follows:
data = csvread('20161102_R05_1.csv',';',1);
and the resulting error (yes, I have skipped the header!)
Error using dlmread (line 138)
HeaderLines must be integer-valued.
Error in csvread (line 47)
m=dlmread(filename, ',', r, c);
Error in Tool_Carrier_Motion_Trial (line 10)
data = csvread('20161102_R05_1.csv',';',1);
dbmn
dbmn 2016년 11월 4일
I just checked with a csv-file that i created by copying your example line 5 times.
Here is what I found
  • csvread gives you the error you specified (most likely due to the fact, that the file is not "real csv" due to the additional commas
  • one solution is to use readtable
data = readtable('20161102_R05_1.csv');
  • Another possibility with more control is to use textscan
fid = fopen('20161102_R05_1.csv','r');
data={};
while ~feof(fid)
data(end+1,:) = textscan(fid, '%s%s%s%s%s', 1, 'delimiter',';');
end
fclose(fid);

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

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by