"Error using textscan: Badly formed format string" with R2012b

2 次查看(过去 30 天)
Hello,
I want to read about 14 million rows of data from an .csv file an save it in a cell array.
The .csv file looks like this:
vehicle_id;_date;measurement_start;measurement_end
100454;2016-09-11T15:01:14.769Z;2.620;6.400
100454;2016-09-11T15:01:15.769Z;7.820;10.400
100452;2016-09-11T15:01:16.769Z;3.620;9.400
100453;2016-09-11T15:01:17.769Z;4.620;5.400
100454;2016-09-11T15:01:18.769Z;20.620;25.400
100458;2016-09-11T15:01:19.769Z;8.620;16.400
100454;2016-09-11T15:01:20.769Z;45.620;50.400
This is my Script to create the cell Array:
clear;
[filename, pathname] = uigetfile({'*.csv','All Files (*.*)'}, 'Pick a file');
filename = [pathname filename];
delimiter = ';';
startRow = 2;
formatSpec = '%f %{yyyy-mm-ddTHH:MM:SSZ}D %f %f %*{^\n}';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
The mistake is in formatSpec when I try to format the Date and Time.
It's one easy mistake, but I can't figure it out.

采纳的回答

Jan
Jan 2017-2-23
编辑:Jan 2017-2-23
Use this instead:
formatSpec = '%f %s %f %f %*{^\n}';
and convert the dates afterwards using datenum. I do not remember if datenum of R2012b can handle this format, but in case of troubles or if this is time critical, you can use FEX: DateStr2Num (use the format spec 31).

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-2-23
The %D format spec was not supported until R2013b.
  2 个评论
Simon Mürwald
Simon Mürwald 2017-2-23
编辑:Simon Mürwald 2017-2-23
This explains a lot, I looked at every function if it is supported, but it didn't came to my mind that %D was not supported :/
If I replace the %D with %s everything works fine, but I get a cell array that looks like this:
dataArray =
[7x1 double] {7x1 cell} [7x1 double] [7x1 double]
Is there a way to get it look like this:
dataArray =
[100454] '2016-09-11T15:01:14.769Z' [ 2.6200] [ 6.4000]
[100454] '2016-09-11T15:01:15.769Z' [ 7.8200] [10.4000]
[100452] '2016-09-11T15:01:16.769Z' [ 3.6200] [ 9.4000]
[100453] '2016-09-11T15:01:17.769Z' [ 4.6200] [ 5.4000]
[100454] '2016-09-11T15:01:18.769Z' [20.6200] [25.4000]
[100458] '2016-09-11T15:01:19.769Z' [ 8.6200] [16.4000]
[100454] '2016-09-11T15:01:20.769Z' [45.6200] [50.4000]
then I could sort it without transforming it.
Walter Roberson
Walter Roberson 2017-2-23
[num2cell(dataArray{1}), dataArray{2}, num2cell(dataArray{3}), num2cell(dataArray{4})]

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by