MATLAB Answers

fwrite with fixed length of different data types

9 views (last 30 days)
Kanchibhotla Chandra Sekhar
Commented: Walter Roberson on 1 Jan 2019
I have a know format file with different reading format. Example -
fread(fid,[1, 40], 'char')
fread(fid,[1, 4], 'long')
fread(fid,[1, 1], 'short')
% These are data blocks
fread(fid,[400 1],'double')
fread(fid,[400 1],'int')
fread(fid,[400 1],'float')
Now, i have to write the data in the same fashion
fwrite(fid,strwrite,'40*char')
something like that -
how can i pad and fwrite of other data structures

Answers (1)

Walter Roberson
Walter Roberson on 26 Dec 2018
40*char is not a permitted fwrite precision .
The precision option is not intended to give information about how many elements are being written: instead it gives information about how each input element is to be represented as output. The number is determined by the size of the variable .
fwrite(fid,strwrite,'char')
If you need to pad then fwrite the pad values yourself .
fwrite(zeroes(1,7),'uint8')
for example .
It is not possible to fwrite two different datatypes in one call. (However memory mapping routines can input or output entire structure at a time .)
  3 Comments
Walter Roberson
Walter Roberson on 1 Jan 2019
Some time ago I wrote a small function to fwrite() with padding or truncation to a given length. It is easy to do and makes it easier to write out fixed-width structures.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!