writing binary data with matlab !

조회 수: 7 (최근 30일)
Sukuchha
Sukuchha 2011년 7월 18일
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?

채택된 답변

Walter Roberson
Walter Roberson 2011년 7월 18일
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
Sukuchha
Sukuchha 2011년 7월 18일
so, with you help i now know where the problem is. Thanks got your point!
God bless you!
Sukuchha
Sukuchha 2011년 7월 19일
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개)

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by