Text file generated using MATLAB has invalid characters in it.

14 views (last 30 days)
Dear community,
I'm writing a MATLAB code that generates input file for GATE simulation (MATLAB R2020a ver.).
The file format is .mac, but it can be editted via notepad in Windows and gedit in Ubuntu environment.
I'm facing a problem that the files generated using my MATLAB code have random invalid characters at the end of the document.
The .zip file attached contains one example of the files with error ("phantom002.mac").
Below is my MATLAB code to generate the files.
%%%% Input variables
Dist = 0.7774;
tic;
for i = 1:700 %%%% Number of .mac files to be generated.
fid_input = fopen('phantom_raw.mac','r+'); %%%% Input .mac file.
fid_output = fopen(['output_phantom/phantom' sprintf('%03d',i) '.mac'],'w'); %%%% Output .mac file.
save(['output_phantom/phantom' sprintf('%03d',i) '.mac']); %%%% Generate output .mac file.
for j = 1:10 %%%% Number of lines in the input .mac file.
temp_line = fgets(fid_input); %%%% Retrieve the (j-1) th line from the input .mac file.
fprintf(fid_output,temp_line); %%%% Write the retrived line into the output .mac file.
if (j == 7) %%%% For the 6 th line,
line = ['/gate/Car/placement/setTranslation ' num2str(275-Dist*(i-1)) ' 0. 12.5 mm'];
fprintf(fid_output,line); %%%% write an array of characters "line" into the output .mac file.
end
end
fclose('all'); %%%% Close both input .mac file and output .mac file.
end
toc;
The strange thing is that, for the first file generated through the loop, it has no invalid character at the end of the document.
That is, if the "for" loop is "for i = 2:700", the corresponding output "phantom002.mac" will have no invalid character, while the following files will have the invalid characters.
As long as in my understanding, "fopen" function with permission "w" ignores the content of the file that is opened.
Hence, the invalid character at the end of the document may be added at the step of saving or closing the output file.
However, I cannot figure out which part is reponsible for the addition of those invalid characters, for the code only reads in 10 lines from the input .mac file, and writes the same number of lines to the output .mac file.
I've tested my code in both Windows and Ubuntu environments, but both of them gave me the output files with invalid characters.
For those who want to test it on their workspace, I attach my code ("phan_raw_gen.m") and input .mac file ("phantom_raw.mac") in the .zip file.
Thanks in advance, and I hope someone has solution for this aching problem.

Accepted Answer

Stephen23
Stephen23 on 1 Jul 2021
Edited: Stephen23 on 1 Jul 2021
Get rid of the SAVE command.
It is not completely clear why you added SAVE, but it is writing binary data into the same file that you opened with FOPEN, the binary data contains all variables that exist in the MATLAB workspace.
"The strange thing is that, for the first file generated through the loop, it has no invalid character at the end of the document."
It is not strange at all: on the first iteration TEMP_LINE and the very badly-named LINE do not exist, so are not written as binary data to that file. On the second and later iterations they (and possibly other variables) do exist and so there is more binary data written to the file, and even after you write some text data to the file using FPRINTF there remains some binary data visible at the end of the file.
  1 Comment
Hwan Hui Jo
Hwan Hui Jo on 1 Jul 2021
Thanks a lot. The code now works properly for me. For the first iteration, the workspace is empty, so no binary data saved into the output file, whereas from the second iteration, the workspace is filled with some data, so corresponding binary data is written in the following files. Now I clearly get it. Thanks for the insight.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!