MATLAB: Writing binary data with matlab !

binaryfreadfwrite fseek

I want to write some values in a binary format so that I can open in a specific software. I want to write it after skipping certain byte (c ) .After that what I wanted to do is write matrix A, B and C which are row vectors of equal size so that follows a1 b1 c1 a2 b2 c2 pattern ! Each data in the matrix is of size 4 bytes. Here what I did !
fid = fopen(test.bin,'wb')
fseek(fid, c, 'bof');
% fwrite use column order while writing therefore I used transpose of A
% skip is 8 because to keep empty 8 bytes which will take by B and C
fwrite(fid,A','int32','ieee-le',8);
fseek(fid, c+4, 'bof');
fwrite(fid,B','int32','ieee-le',8);
fseek(fid, c+8, 'bof');
fwrite(fid,C','int32','ieee-le',8);
fclose(fid);
Is this a correct way to do ? Anybody has any idea?? When I try to get data from the binary file to check if the data has been properly written! I couldnot retrieve matrix A, B and C
fid = fopen('A.bin','rb') %open file
fseek(fid, c, 'bof');
data_A = fread(fid, inf, 'int32',8);
fseek(fid, c+4, 'bof');
data_B = fread(fid, inf, 'int32',8);
fseek(fid, c+8, 'bof');
data_C = fread(fid, inf, 'int32',8);
fclose(fid);
The output of data_A, data_B, data_C should same as original matrix A, B, C. In my case it is not ? what I am doing wrong ! Sukuchha?

Best Answer

What datatype are your matrices? You should consider using '*int32' instead of 'int32' in your formats, if the datatypes involved are int32 .
Are your matrices row vectors or are they not row vectors? If they are row vectors then the transpose at the time of writing is irrelevant, but at the time of reading you would want [1 inf] instead of just [inf] as the size.
If A, B, and C are row vectors, then what I would suggest is
fid = fopen(test.bin,'wb')
fseek(fid, c, 'bof');
fwrite(fid, [A;B;C], '*int32');
This will have the effect of doing the interleaving you want.
Related Question