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 日
Upload your file by clicking the paper clip button.
madhan ravi
madhan ravi 2018 年 8 月 24 日
can you upload your code?
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 日

0 投票

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 日
It seems your local settings are different from mine, so readtable interprets the first column as datetime instead of duration. Add these two lines at the beginning of the loop.
opts=detectImportOptions(files(i).name)
opts=setvartype(opts,'Var1','duration')
jonas
jonas 2018 年 8 月 24 日
編集済み: jonas 2018 年 8 月 24 日
I'm having trouble reproducing the issue. At what line does it say that?
type this and paste the output in comment here:
varfun(@class,data,'OutputFormat','cell')
jonas
jonas 2018 年 8 月 24 日
編集済み: jonas 2018 年 8 月 24 日
Alright, what release are you running? We may have to convert the cell to duration subsequent to loading the table. Copy a few rows from the first column here so I can figure out how to convert it. In the meantime, you can try:
TT=timetable(data)
as timetable is sometimes smart enough to figure out the format of the input.
jonas
jonas 2018 年 8 月 24 日
編集済み: jonas 2018 年 8 月 24 日
Let's skip readtable altogether and try another import option:
files=dir('folderpath~\*.csv');
for i=1:length({files.name})
date=files(i).name;
date=regexprep(date,'.csv','')
din=importdata(files(i).name,';')
t{i}=duration(din.textdata,'format','hh:mm:ss')+datetime(date);
xy{i}=din.data
end
I wouldn't recommend your other solution. Albeit it could work, it is the opposite of robust.
jonas
jonas 2018 年 8 月 24 日
Seems to have been more updates to DURATION than I was aware of but alright, give me a few minutes to fix
Walter Roberson
Walter Roberson 2018 年 8 月 24 日
In this situation, let it be imported as datetime, but then do
t{i} = data.Var1 - dateshift(data.Var1, 'start', 'day') + datetime(day);
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 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

タグ

質問済み:

2018 年 8 月 24 日

編集済み:

2018 年 8 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by