Matlab's fwrite: What happens to skipped bytes?

In these kind of question, the easiest way to find out is usually to try it out yourself if it's not too complex. Now you indicated you are using Linux, may be you can repeat the test on your platform and see if the results match.


For Windows platform, the skip value:

  • is implemented before the first value to write
  • seems to leave every skipped byte at value 00 (probably what the OS assign as new value for a file)

Examples:

This code:

fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,1,'short',10,'ieee-le')
fclose(fid)

Produces the following file (seen in a hex editor): enter image description here

And if you have more than one value to write:

fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,[1 2 3],'short',10,'ieee-le')
fclose(fid)

You still get 10x 00 values before each short value you actively write: enter image description here


This was the case for a newly created file. Let's see what happend to an existing file:

%% Let's create a file full of `FF`
FFarray = uint8(ones(36,1)*255) ;
fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,FFarray,'uint8')
fclose(fid)

which gives: enter image description here

Now by using the same code as before (with the permission set to w):

fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,[1 2 3],'short',10,'ieee-le')
fclose(fid)

enter image description here Yes, we still get the same thing. Now this is consistent with the MATLAB documention for the permission you indicated:

w => Open or create new file for writing. Discard existing contents, if any.

If you simply change that permission to r+ (Open file for reading and writing.):

fid = fopen(my_filename,'r+','ieee-le','ISO-8859-1');
fwrite(fid,[1 2 3],'short',10,'ieee-le')
fclose(fid)

You only overwrite the non-skipped values: enter image description here