我正在尝试阅读一堆文本文件。有一个日期列。日期列的某些文件中的格式为DD-MMM-YYYY
,而在其他文件中,格式为DD-MM-YYYY
。我已将代码设置为读取第一个样式。但正因为如此,如果它遇到第二种类型,代码会停止,因为它无法读取文件。我该怎么做If the
textscan doesn't work, try this second way
?
for n = 1:length(data1{id})
fname1 = char(data1{id}(n));
delimiter = '\t';
startRow = 2;
formatSpec = '%s%f%f%f%s%s%s%s%{dd-MMM-yyyy}D%s%s%f%f%f%f%f%f%s%s%s%s%s%s%s%s%f%f%[^\n\r]';
fileID = fopen(fname1,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID); % Close the text file.
PM25_1{id}{n} = table(dataArray{1:end-1}, 'VariableNames', {'MonitorID','POC','Latitude','Longitude','Datum','ParameterName','SampleDuration','PollutantStandard','DateLocal','UnitsofMeasure','EventType','ObservationCount','ObservationPercent','ArithmeticMean','FirstMaxValue','FirstMaxHour','AQI','MethodName','LocalSiteName','Address','StateName','CountyName','CityName','CBSAName','DateofLastChange','DateNum','NumberOfPOCs'});
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
end
答案 0 :(得分:2)
try
for n = 1:length(data1{id})
fname1 = char(data1{id}(n));
delimiter = '\t';
startRow = 2;
formatSpec = '%s%f%f%f%s%s%s%s%{dd-MMM-yyyy}D%s%s%f%f%f%f%f%f%s%s%s%s%s%s%s%s%f%f%[^\n\r]';
fileID = fopen(fname1,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID); % Close the text file.
PM25_1{id}{n} = table(dataArray{1:end-1}, 'VariableNames', {'MonitorID','POC','Latitude','Longitude','Datum','ParameterName','SampleDuration','PollutantStandard','DateLocal','UnitsofMeasure','EventType','ObservationCount','ObservationPercent','ArithmeticMean','FirstMaxValue','FirstMaxHour','AQI','MethodName','LocalSiteName','Address','StateName','CountyName','CityName','CBSAName','DateofLastChange','DateNum','NumberOfPOCs'});
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
end
catch
for n = 1:length(data1{id})
fname1 = char(data1{id}(n));
delimiter = '\t';
startRow = 2;
formatSpec = '%s%f%f%f%s%s%s%s%{dd-MM-yyyy}D%s%s%f%f%f%f%f%f%s%s%s%s%s%s%s%s%f%f%[^\n\r]';
fileID = fopen(fname1,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID); % Close the text file.
PM25_1{id}{n} = table(dataArray{1:end-1}, 'VariableNames', {'MonitorID','POC','Latitude','Longitude','Datum','ParameterName','SampleDuration','PollutantStandard','DateLocal','UnitsofMeasure','EventType','ObservationCount','ObservationPercent','ArithmeticMean','FirstMaxValue','FirstMaxHour','AQI','MethodName','LocalSiteName','Address','StateName','CountyName','CityName','CBSAName','DateofLastChange','DateNum','NumberOfPOCs'});
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
end
end
将所有内容包裹在try/catch
块中。如果第一个样式失败,请尝试下一个样式(请注意,我更改了catch
部分中的日期格式。)如果您有更多可能性,则需要使用类似{{{{{{ 1}}子句。