سؤال

I have a file that looks like this:

RD|Action Code|State Code|County Code|Site ID|Parameter|POC|Sample Duration|Unit|Method|Date|Start Time|Sample Value|Null Data Code|Sampling Frequency|Monitor Protocol (MP) ID|Qualifier - 1|Qualifier - 2|Qualifier - 3|Qualifier - 4|Qualifier - 5|Qualifier - 6|Qualifier - 7|Qualifier - 8|Qualifier - 9|Qualifier - 10|Alternate Method Detectable Limit|Uncertainty
RC|Action Code|State Code|County Code|Site ID|Parameter|POC|Unit|Method|Year|Period|Number of Samples|Composite Type|Sample Value|Monitor Protocol (MP) ID|Qualifier - 1|Qualifier - 2|Qualifier - 3|Qualifier - 4|Qualifier - 5|Qualifier - 6|Qualifier - 7|Qualifier - 8|Qualifier - 9|Qualifier - 10|Alternate Method Detectable Limit|Uncertainty
RD|I|01|101|1002|88501|3|1|105|733|20130101|00:00|11.1|||||||||||||||
RD|I|01|101|1002|88501|3|1|105|733|20130101|01:00|16.7|||||||||||||||
RD|I|01|101|1002|88501|3|1|105|733|20130101|02:00|17.7|||||||||||||||
RD|I|01|101|1002|88501|3|1|105|733|20130101|03:00|15.7|||||||||||||||
RD|I|01|101|1002|88501|3|1|105|733|20130122|10:00||BJ||||||||||||||
RD|I|01|101|1002|88501|3|1|105|733|20130122|11:00|6.5|||||||||||||||
RD|I|01|101|1002|88501|3|1|105|733|20130122|12:00|6.3|||||||||||||||
RD|I|01|101|1002|88501|3|1|105|733|20130122|13:00|0|||||||||||||||
RD|I|01|101|1002|88501|3|1|105|733|20130122|14:00|-0.2|||||||||||||||

The file goes on for much longer, but the general idea is that there are 2 lines of headers and then the data delimited by '|'.

I've been trying to skip the header lines, but nothing I tried worked. I tried the following script to read in the rest of the file when the header was deleted. The first two columns of the first row worked, but the rest were empty.

How should I edit my code so it will skip the header and read through all the lines of the data? I'm pretty sure I don't have the %d parts correct.

fid = fopen('file.txt','rt');
data = textscan(fid, '%s%s|%s|%d%d|%d|%d|%d|%d|%d|%d|%d|%d:%d|%f|%s||||||||||||||', 'HeaderLines', 4, 'Delimiter', '|');
fclose(fid);

% Divide data and save into variable
[RD,Action_Code,State_Code,County_Code,Site_ID,Parameter,POC,Sample_Duration,Unit,Method,Date,Start_Time,Sample_Value,Null_Data_Code,Sampling_Frequency,Monitor_Protocol_MP_ID,Qualifier_1,Qualifier_2,Qualifier_3,Qualifier_4,Qualifier_5,Qualifier_6,Qualifier_7,Qualifier_8,Qualifier_9,Qualifier_10,Alternate_Method_Detectable_Limit,Uncertainty] = deal(data{:});
هل كانت مفيدة؟

المحلول

I think you want something like this.

Will those last columns always be blank? I assumed no and just put them in the format string.

I left out manipulation of the data after textscan gives you each line, but that should be straight forward.

fid = fopen('test.txt','rt');
format='%s %s %d %d %d %d %d %d %d %d %d %d:%d %f %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d';
data=textscan(fid,format,'HeaderLines',2,'Delimiter','|','EmptyValue',0);
fclose(fid);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top