edit data file with text headers with MATLAB

1 view (last 30 days)
Luis
Luis on 27 Mar 2013
I am trying to edit a text file and overwrite it. The text file has 2 headers lines, thought they don´t correspond to the name of the columns. First column is Time.
#1
double UserProfilesOffice(336, 5)
0 0 0.1 0 0
3540 0 0.1 0 0
3600 0 0.1 0 0
7140 0 0.1 0 0
7200 0 0.1 0 0
10740 0 0.1 0 0
10800 0 0.1 0 0
. . . . .
. . . . .
. . . . .
129540 0.8 0.8 0.3 0.3
129600 0.4 0.8 0 0
133140 0.4 0.8 0 0
133200 0.6 0.8 0.3 0.3
136740 0.6 0.8 0.3 0.3
136800 0.8 0.8 0.3 0.3
140340 0.8 0.8 0.3 0.3
I have to edit only second column data each 7200sec and the next value in second column should be the same, that means multiples of 7200sec then I change it:
129600 0.45 0.8 0 0
133140 0.45 0.8 0 0
Another consideration is that rows are repeated for columns (2,3,4,5) each 86400sec. Then, the program should assign 86400/7200=12 Parameters. I tried the next:
p1=0; p5=0.2; p9=0.3; p13=0.5; p17=0.5; p21=0.6; p25=0.4; p29=0.3; p33=0.2; p37=0.1; p45=0; p49=0; % Value of the 12 parameters to assign
fid = fopen('file.txt','r'); %# Open the file
topLines = 3; %# Keep 2 headers lines
for ii = 1:topLines-1
fgets(fid);
data = textscan(fid, '%d %f %f %f %f\n','HeaderLines',2); % Read the data
fclose(fid); %# Close the file
fid = fopen('file.txt','w'); %# Open the file
fprintf(fid,'%s',topLines); %# Print the top lines
for i = 1:size(data) %# Loop over the rows of data
if ((7200 * round(double(data{1}(i)))/7200) == (data{1}(i))) && ((data{1}(i))<=86400) %To determine if (data{1}(i) is a multiple of 7200 and it is in weekdays
for ii=1:4:49
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii),p(i),data{3}(ii),data{4}(ii),data{5}(ii)); %# Print data i
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii+1),p(i),data{3}(ii+1),data{4}(ii+1),data{5}(ii+1)); %# Print data i+1
end
else if ((7200 * round(double(data{1}(i)))/7200) == (data{1}(i))) && ((data{1}(i))<=172800)
for ii=50:4:98
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii),p(i),data{3}(ii),data{4}(ii),data{5}(ii));
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii+1),p(i),data{3}(ii+1),data{4}(ii+1),data{5}(ii+1));
end
else if ((7200 * round(double(data{1}(i)))/7200) == (data{1}(i))) && ((data{1}(i))<=259200)
for ii=100:4:147
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii),p(i),data{3}(ii),data{4}(ii),data{5}(ii));
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii+1),p(i),data{3}(ii+1),data{4}(ii+1),data{5}(ii+1));
end
else if ((7200 * round(double(data{1}(i)))/7200) == (data{1}(i))) && ((data{1}(i))<=345600)
for ii=148:4:196
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii),p(i),data{3}(ii),data{4}(ii),data{5}(ii)); %# Print the data
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii+1),p(i),data{3}(ii+1),data{4}(ii+1),data{5}(ii+1));
end
else if ((7200 * round(double(data{1}(i)))/7200) == (data{1}(i))) && ((data{1}(i))<=432000)
for ii=197:4:245
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii),p(i),data{3}(ii),data{4}(ii),data{5}(ii));
fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(ii+1),p(i),data{3}(ii+1),data{4}(ii+1),data{5}(ii+1));
end
else fprintf(fid,'%d %.1f %.1f %.1f %.1f %.1f\n',data{1}(i),data{2}(i),data{3}(i),data{4}(i),data{5}(i));
end
end
end
end
end
end
fclose(fid); %# Close the file
Thank you for your time.

Answers (0)

Community Treasure Hunt

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

Start Hunting!