writing binary data with matlab !

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?

 采纳的回答

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.

10 个评论

Walter you're too fast.
Thank you for taking time to reply. I tried you suggestion but it shows some error.
A= int32(rand(10,1)*10);
B= int32(rand(10,1)*10);
C= int32(rand(10,1)*10);
c=10;
fid = fopen('test.bin','wb')
fseek(fid, c, 'bof');
fwrite(fid, [A;B;C], '*int32');
fclose(fid);
??? Error using ==> fwrite
Invalid precision.
Ah, omit the '*' in the precision (which makes sense now that I think about it.)
ok, when i omit * in precission, the data is written but when i check with fread i dont get same matrix A B C.
fid = fopen('test.bin','rb') %open file
fseek(fid, c, 'bof');
data_A = fread(fid, [1 inf], 'int32');
fclose(fid);
fid = fopen('test.bin','rb')
fseek(fid, c, 'bof');
data1_A = fread(fid, inf, 'int32',8);
fseek(fid, c+4, 'bof');
data1_B = fread(fid, inf, 'int32',8);
fseek(fid, c+8, 'bof');
data1_C = fread(fid, inf, 'int32',8);
fclose(fid);
In both case, i dont get the original A,B and C matrix! i going crazy :(
You didn't use row vectors, you used column vectors. Pick one and stick to it, please. For column vectors, you would
fwrite(fid, [A,B,C].', 'int32')
You have a basic problem, though, that fseek() beyond the end of file is failing, at least as of 2008b! This is contrary to POSIX.1, which defines the consequences in terms of fseek() past end of file in terms of the behaviour of lseek(), with POSIX.1-1990's lseek() documented as
The lseek() function shall allow the file offset to be set beyond the end of existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value zero until data is actually written in to the gap.
I am not sure if we have an an active support contract or not; I will have to check that before I can report this issue.
Also you should still be using '*int32' for your fread(), just not for your fwrite().
sorry for my mistake ! i tried again with A= int32(rand(1,10)*10) and so on and also with '*int32' for fread() but still i cannot get the original A,B and C matrix.
Mr roberson, please once you figured out the problem, let me know !
As indicated, the basic problem is that you cannot fseek() past the end of file. You can work around this as
fid = fopen('test.bin','wb');
fwrite(fid, zeros(1,c,'uint8'),'uint8');
and then do your fwrite() of A, B, and C, without any fseek()
so, with you help i now know where the problem is. Thanks got your point!
God bless you!
Roberson, when I thought everything is sorted out, I ran into another problem. Let say I have four matrix A,B, C and D which are all row vectors but of different data types. A, B and C are of types int32 and D is of type uint16. Now how do I write the matrix in noncontiguous fields? I cannot join matrix A,B,C,D as ABCD = [A;B;C;D] because when I write ABCD I can specific only one datatype.
%A is 4 bytes
A= int32(rand(10,1)*10);
%B is 4 bytes
B= int32(rand(10,1)*10);
%C is 4 bytes
C= int32(rand(10,1)*10);
%D is 2 bytes
D= uint16(rand(10,1)*10);
%ABCD = [A;B;C;D];
%
fid = fopen('test.bin','wb') %open file
fseek(fid, 0, 'bof');
%ABCD = [A;B;C;D]; ABCD becomes int32 type
%fwrite(fid,ABCD, 'XXXXX'); % ABC are data type int32 but D is uint16.?
%how to specify that ??
fwrite(fid,A, 'int32',10);
fseek(fid, 4, 'bof');
fwrite(fid,B, 'int32',10);
fseek(fid, 8, 'bof');
fwrite(fid,C, 'int32',10);
fseek(fid, 10, 'bof');
fwrite(fid,D, 'uint16',10);
fclose(fid);
fid = fopen('test.bin','rb')
fseek(fid, 0, 'bof');
data_A = fread(fid, [1 inf], 'int32',10);
fseek(fid, 4, 'bof');
data_B = fread(fid, [1 inf], 'int32',10);
fseek(fid, 8, 'bof');
data_C = fread(fid, [1 inf], 'int32',10);
fseek(fid, 10, 'bof');
data_D = fread(fid,[1 inf], 'uint16',10);
fclose(fid);

请先登录,再进行评论。

更多回答(0 个)

类别

Community Treasure Hunt

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

Start Hunting!

Translated by