Textscan - 捕获错误并尝试其他方法
Textscan - Catch error and try something else
我正在尝试读入一堆文本文件。有一个日期栏。某些文件中日期列的格式为 DD-MMM-YYYY
,而在其他文件中为 DD-MM-YYYY
。我已将代码设置为读取第一种样式。但正因为如此,如果它遇到第二种类型,代码就会停止,因为它无法读取文件。我该怎么做,If the
textscandoesn'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
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
部分中的日期格式。)如果您有更多的可能性,您将需要使用 [= 之类的东西检查每种样式13=] 子句。
我正在尝试读入一堆文本文件。有一个日期栏。某些文件中日期列的格式为 DD-MMM-YYYY
,而在其他文件中为 DD-MM-YYYY
。我已将代码设置为读取第一种样式。但正因为如此,如果它遇到第二种类型,代码就会停止,因为它无法读取文件。我该怎么做,If the
textscandoesn'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
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
部分中的日期格式。)如果您有更多的可能性,您将需要使用 [= 之类的东西检查每种样式13=] 子句。