Error using textscan Badly formed format string??

4 次查看(过去 30 天)
Hello, I'm trying to plot the csv file (attached) but if keeps giving me the error:
"Error using textscan
Badly formed format string.
Error in johnvelasco_hw_3 (line 9).."
(line 9) which is..:
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue', NaN,'HeaderLines', startRow-1, 'ReturnOnError', false);\
the complete code is
clear all;
close all;
clc;
filename = 'C:\Users\jvelasco2009\Downloads\hw3\CO-OPS__8722670__wl.csv';
delimiter = ',';
startRow = 2;
formatSpec = '%{yyyy-mm-dd HH:mm}D%f%f%f%f%f%f%g%{^\n\r}';
fileID = fopen(filename, 'r');
dataArray = textscan(fileID, formatSpec, ...
'Delimiter', delimiter, ...
'EmptyValue', NaN, ...
'HeaderLines', startRow-1, ...
'ReturnOnError', false);
fclose(fileID);
DateTime = dataArray{:,1};
WaterLevel = dataArray{:, 2};
Sigma = dataArray {:, 3};
O = dataArray{:,4};
F = dataArray{:,5};
R = dataArray{:,6};
L = dataArray{:,7};
Quality = dataArray{:,8};
clearvars filename delimiter startRow formatSpec fileID dataArray one;
x = DateTime;
y = WaterLevel;
plot (x,y)
xlabel('Date and Time (08/01/2015-08/31/2015)')
ylabel('Water Level')
title('Tide Chart Lake Worth Pier, FL')
Mean = mean(WaterLevel)
I'd really appreciate the help, im [sic] stuck and I gotta turn this in soon. thank you!

回答(1 个)

dpb
dpb 2015-9-14
formatSpec = '%{yyyy-mm-dd HH:mm}D%f%f%f%f%f%f%g%{^\n\r}';
The curly brackets don't belong around the trailing %{^\n\r} to skip remainder of line; use square brackets there instead. There are six numeric fields following the date column in the file which match the %f but the last column is character so the %g will fail. If you don't want that column, I'd use %*s for it altho the skip rest of line will work if just delete the %g.
To debug, these kinds of things, copy a line of the file to a string variable in the command window and then can use textscan on that string to debug a little at a time by parsing pieces of the string at a time.
formatSpec = '[%{yyyy-mm-dd HH:mm}D' repmat('%f',1,6) '%*s'];
Testing here (altho my release doesn't include the datetime class so I read the date fields as strings)...
>> s='08/31/2015 7:42 0.031 0.084 0 0 0 0 v';
>> fmt = ['%s %s' repmat('%f',1,6) '%*s'];
>> textscan(s,fmt,'collectoutput',1)
ans =
{1x2 cell} [1x6 double]
>> ans{:}
ans =
'08/31/2015' '7:42'
ans =
0.0310 0.0840 0 0 0 0
>>
Voila!!! NB: I also used 'collectoutput' to minimize the cell arrays into two instead of several...
  2 个评论
Walter Roberson
Walter Roberson 2015-9-14
Also note that %D formats are new with R2014b so if you have an older release you will not be able to use %D formats.
dpb
dpb 2015-9-14
That was why I read as strings--I presume the D format string descriptor was introduced with the datetime class, Walter, although it could theoretically have been applied for conversion directly to date numbers even as doubles--would been useful on occasion there as well.

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by