0
votes

I'm trying to read in a bunch of text files. There's a date column. The format in some of the files for the date column is DD-MMM-YYYY while in others, it's DD-MM-YYYY. I have the code set up to read the first style. But because of that, if it runs into the second type, the code stops because it can't read the file. How can I do something like, If thetextscandoesn'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
1

1 Answers

2
votes
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

Wrap everything in a try/catch block. If the first style fails, try the next one (note that I changed the date-format in the catch part.) If you have even more possibilities, you'll want to check each style with something like an if/else clause.