Import CSV file using readtable gives wrong date time format.
Show older comments
Hi,
I am having a little problem setting the format for a variable when I use read table, it is a similar problem to others posted before, but I couldn't understand the responses posted fully.
here is my csv format (only one line, the date is 5th april! not the 4th of may)
05/04/2019 11:43,-712.6206,-204.0489,22.9026,22.3949,22.3029,22.5494,22.6193
I set the first column and wrote it to a csv file previously using
fprintf(fid, string(datetime('now')));
How can I set the import to be in european format not american, (its displaying wrong when I use the data to plot graphs.
Current code is
Table=readtable(fullfile(path,file))
DATAdate=table2array(Table(:,1));
Not really sure how to use the format arguements.
Thanks!
Steve
Accepted Answer
More Answers (1)
Bob Thompson
on 9 Apr 2019
0 votes
In 'newer' versions of Matlab (sorry, I don't know when this started) it is possible to define a datetime format as a named card within the readtable command. I haven't tested it, but according to the documentation it should look something like this:
Table=readtable(fullfile(path,file),'DatetimeType','dd/mm/yyyy hh:mm');
2 Comments
Steven Brace
on 9 Apr 2019
Bob Thompson
on 9 Apr 2019
I suspect the issue is with all of the other bits of data, rather than the first column. Without testing this though I cannot be sure.
Because you have the mixed format, you may be better off conducting a textscan so that you can set the format of each of the different elements you are retrieving. This will also put the results directly into an array as well.
nc = 7; % Number of columns of data (not including time)
fmt = ['%s,',repmat('%7.4f,',1,nc)]; % Create format string
data = textscan(fullfile(path,file),fmt); % Might need to add 'delimiter',',' to this command
data(:,1) = datetime(data(:,1),'dd/mm/yyyy hh:mm');
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!