Import CSV data into Matlab
3 views (last 30 days)
Show older comments
I am trying to import data CSV using MATLAB, with patient_1(1).Data, as a following:
to import I used this code:
filedir = '/Users/'; files1 = dir(fullfile(filedir, '*.csv')); numFiles1 = length(files1); for i = 1 : numFiles1 patient_1(i).Name = files1(i).name; patient_1(i).Data = readtable(fullfile(filedir,files1(i).name),'ReadVariableNames',false); % loads data end
But I got this warning and following error:
Warning: Unable to determine the format of the DURATION data. Try adding a format to the DURATION specifier. e.g. '%{hh:mm:ss}T'. > In table/readTextFile>textscanReadData (line 554) In table/readTextFile (line 225) In table.readFromFile (line 39) In readtable (line 197) In patient1 (line 23) Error using readtable (line 197) Unable to determine the format of the DURATION data. Try adding a format to the DURATION specifier. e.g. '%{hh:mm:ss}T'. Note: readtable detected the following parameters: 'Delimiter', ';', 'HeaderLines', 0, 'Format', '%q%q%T%q%q%T%T%f%f%q%q' Error in patient1 (line 23) patient_1(i).Data = readtable(fullfile(filedir,files1(i).name),'ReadVariableNames',false); % loads data
Can anyone help me to solve this problem?
4 Comments
Walter Roberson
on 4 Jul 2018
patient_1(i).Data = readtable(fullfile(filedir,files1(i).name), 'ReadVariableNames', true, 'HeaderLines', 1);
A small number of lines for the test file would be fine. I extracted some of the data from the image you posted but I am not encountering the message you are getting.
Accepted Answer
Walter Roberson
on 4 Jul 2018
opt = detectImportOptions('EE_moderate.csv');
opt = setvartype(opt, [2 5], 'datetime');
opt = setvaropts(opt, [2 5], 'InputFormat', 'dd-MMM yyyy', 'DatetimeFormat', 'eee uuuu-MM-dd HH:mm:ss' );
...
T = readtable(fullfile(filedir,files1(i).name), opt);
T.date = T.date + T.time;
T.date_1 = T.date_1 + T.time_1;
patient_1(i).Data = T(:,[2 5 7:end]);
...
19 Comments
Walter Roberson
on 16 Jul 2018
Not all of your files have a field #10.
In the original file, field #10 contained percentages complete with the % character. detectImportOptions sees the % and figures that the entire field must be a character vector rather than a number. Doing the setvartype() is to force it to treat as a number, with the % character taken care of by way of the Suffixes option.
I think I asked you earlier what other special characters might occur in fields that you want treated as numeric.
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!