Changing the header in a file which include binary content

My ply file contains an ascii header and the rest of the mesh data content is written as binary. I would like to change lines 10, 11, 12 in the header with my specific content. I do not want to touch to the binary data which contains the mesh data. When I use the script below, my mesh data becomes like a flat surface instead of a 3D surface curvature. Why am I changing the rest of the content? How can I change the three header lines, without damaging the rest of the file?
A = regexp( fileread(ply), '\n', 'split');
A{10} = sprintf('%s',"header line 1");
A{11} = sprintf('%s',"header line 2");
A{12} = sprintf('%s',"header line 3");
fid = fopen(ply, 'wb');
fprintf(fid, A{:});
fclose(fid);

 Respuesta aceptada

For a start, you never put back all the \n characters that you've removed.
fprintf(fid, strjoin(A, '\n'));
may work. However, you're playing with fire here as you're interpreting binary data as text. There's no guarantee that the binary data will survive the round trip unaltered (e.g. the binary data forms an invalid character that could be ignored by regex).
And of course, it's possible that he binary data includes offset indicating where to find other binary data into the file, if you change the length of the header the offsets will point to the wrong location.
It's very unusual to have a file that's text and binary. It may be that some of the binary content is some textual data, but it should still be interpreted as binary. Usually text in a binary file is either fixed length, prefixed by binary data indicating how long the text is, or \0 terminated.

6 comentarios

Thanks for your suggestion. strjoin() worked for the header part, it didn't work for the binary part. For some reason, I have only one line binary and the rest is not written back.
At this point, the best would be for you to provide an example file and if possible, a link to a document specifying the format of the file.
As both Walter and I said, treating the binary part as text is dangerous anyway.
Thanks for your suggestion.
One example file can be found at this link.
These following 3 lines should be replaced with lines which are defined by me;
property uchar diffuse_red
property uchar diffuse_green
property uchar diffuse_blue
You can give me an example, by changing these lines with "line1", "line2", "line3" for instance.
Does the attached file show the problem as well?
It was created with this code:
fid = fopen('result2.0.ply', 'r');
text = {];
while true
tline = fgetl(fid);
assert(ischar(tline), 'end of file reached prematurely');
text = [text; {tline}];
if strcmp(tline, 'end_header')
break;
end;
end;
binarydata = fread(fid);
fclose(fid);
text(11:13) = {'line 1'; 'line 2'; 'line3'}
fid = fopen('newresult.ply', 'w');
fprintf(fid, '%s\n', strjoin(text, '\n'));
fwrite(fid, binarydata);
fclose(fid);
I cannot thank you enough! This does the work! Except a tiny typo at the second line :)
text = [];
Very great thanks!
Well, it was meant to be:
text = {};
but either work.

Iniciar sesión para comentar.

Más respuestas (1)

When you split, the newlines are removed, and you are not putting them back.
fprintf(fid, '%s\n', A{1:end-1});
fprintf(fid, '%s', A{end}); %do not add extra \n at the end
but splitting the binary content is kinda dicey.
I would recommend that you instead switch to reading a limited number of lines using fgetl(), and write out the revised versions as required, and then fread() to end of file and fwrite() it to the new file.

1 comentario

Thank you very much for your quick response. I have immediately tried out but I still have the same result. The mesh values are changed. The 3D structure becomes flat.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 11 de Abr. de 2018

Comentada:

el 13 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by