Wrong date while importing .csv

조회 수: 7 (최근 30일)
Ancalagon8
Ancalagon8 2018년 8월 24일
편집: jonas 2018년 8월 24일
Having a problem when i import daily .csv files. More specifically, when i plot my xy data(x axis contains date and time stamp), in every different daily file, MATLAB shows todays date. Any ideas?
  댓글 수: 4
madhan ravi
madhan ravi 2018년 8월 24일
Just lookup xticklabels.
Stephen23
Stephen23 2018년 8월 24일
편집: Stephen23 2018년 8월 24일
@Sirius8: this is what one line of the file looks like:
00:00:0;0.0;0.00540500736377
Where is the date? Please show us which parts of that line contain date data.

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

채택된 답변

jonas
jonas 2018년 8월 24일
편집: jonas 2018년 8월 24일
The date is not automatically assigned because it's not in the data but in the name of the file. You could do something like this:
files=dir('folderpath~\*.csv');
for i=1:length({files.name})
date=files(i).name;
date=regexprep(date,'.csv','')
data=readtable(files(i).name);
t{i}=data{:,1}+datetime(date);
xy{i}=data{:,2:3}
end
Now you have two cell arrays, t and xy, where the former has all time data and the latter all other data.
  댓글 수: 8
jonas
jonas 2018년 8월 24일
편집: jonas 2018년 8월 24일
Thanks Walter, that should do the trick.
If it, for some reason, still does not work. Then this solution should. It's however much slower due to the cellfun
files=dir('folderpath~\*.csv');
for i=1:length({files.name})
date=files(i).name;
date=regexprep(date,'.csv','')
din=importdata(files(i).name,';')
tday=cellfun(@(x) str2double(strsplit(x,':')),din.textdata,'uniformoutput',false)
tday=cell2mat(tday);
t{i}=hours(tday(:,1))+minutes(tday(:,2))+seconds(tday(:,3))+datetime(date);
xy{i}=din.data
end
The absolute best solution would be to update MATLAB :)
jonas
jonas 2018년 8월 24일
편집: jonas 2018년 8월 24일
You're welcome! Note that you can just remove the curly braces from the t{i} if you want to achieve that. I put the results in a cell array as I figured you have several data sets that you want to load and store.
Also, don't use datetime as a variable name!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by