Since this line gets executed in each cycle, you open the file 36000
times, which likely causes your problem.
Either put a fclose after the following fprintf calls or better open
fh2 outside the loop (I see no need to reopen it).
Btw., you can probably write a much simpler and faster code by using
more vectorization. Also, it seems your code indexes out of bounds in
x.
*******
thanks very much for your response. it is very useful for me. I fixed my program based on your clue. I tried to put fh2 outside the loop, but I didn't get the output file. So I put the fh2 inside the loop and added the fclose after fprintf and endfor command. Fortunately, I can get the 3600 lines data in the output file, but I met some error on the terminal.
the error is :
error: invalid column index = 36010
error: evaluating assignment _expression_ near line 4, column 3
error: evaluating for command near line 3, column 1
In my mind, I think the error means the program read input file again after the 36000 lines, I have no idea how to stop it.
Octave is my first program on the linux environment, so I have no idea how to make the program much faster and simpler by using more vectorization. Maybe, are there the best links or websites for me to learn about it by myself?
Thanks very much in advance.
This my complete program :
#!/bin/bash
for year in 2008
do
for month in 08
do
for day in 26
do
for hour in 01
do
for min in 00
do
for sec in 00
do
octave -q <<EOF
fid=fopen("${year}${month}${day}${hour}${min}${sec}.dat","r");
#x=fscanf(fid,"%lf", [8 36000]);
x=fwrite(fid,"%lf", [8, 36000]);
for i=0:36000
x2=x(2,i*10+1:i*10+10);
x3=x(3,i*10+1:i*10+10);
x4=x(4,i*10+1:i*10+10);
x5=x(5,i*10+1:i*10+10);
x6=x(6,i*10+1:i*10+10);
x7=x(7,i*10+1:i*10+10);
x8=x(8,i*10+1:i*10+10);
x9=mean(x2);
x10=mean(x3);
x11=mean(x4);
x12=mean(x5);
x13=mean(x6);
x14=mean(x7);
x15=mean(x8);
fh2=fopen("${year}${month}${day}${hour}${min}${sec}.1Hzc.dat","a+");
fprintf(fh2,'%f',x9);
fprintf(fh2,'%10f',x10);
fprintf(fh2,'%10f',x11);
fprintf(fh2,'%10f',x12);
fprintf(fh2,'%10f',x13);
fprintf(fh2,'%10f',x14);
fprintf(fh2,'%10f\n',x15);
fclose(fh2);
endfor;
fclose(fid);
EOF
done
done
done
done
done
done