Hi Francesco,
My understanding is that you have a text file with some lines beginning with "PE02" and you want a workaround to modify this line in the original file using "fprintf" function.
There are two issues in the code you shared-
- The modified line appears after the line intended to be modified.
- This makes the next line read using "fgetl" function non char array and the script unexpectedly terminates.
Here's a workaround that involves writing into a temporary file first and substituting the content with the original file at the end.
abb_sat = "PE02";
filename = strcat('C:\multiGNSS_v3\mgnssPre\ephConv\', out_sp3_abb, '\', num2str(YR+2000), '\', out_sp3_abb, num2str(WN), num2str(DoW), est);
tempFilename = 'temp.txt';
FID = fopen(filename, 'r');
FID_temp = fopen(tempFilename, 'w');
line = fgetl(FID);
x_sp3 = -10000.666666;
y_sp3 = -10.666666;
z_sp3 = -1000.666666;
dc_sp3 = -0.666666;
while ischar(line)
if ~isempty(strfind(line, abb_sat))
fprintf(FID_temp, '%.5s %13.6f %13.6f %13.6f %13.6f\n', abb_sat, x_sp3, y_sp3, z_sp3, dc_sp3);
else
fprintf(FID_temp, '%s\n', line);
end
line = fgetl(FID);
end
fclose(FID);
fclose(FID_temp);
fclose('all');
movefile(tempFilename, filename);
Hope this helps !
Regards
Vinayak Luha