Is it possible to create a binary vector then write it to a file?

조회 수: 6 (최근 30일)
Tai Doan
Tai Doan 2016년 5월 20일
답변: Walter Roberson 2016년 5월 20일
Hi Matlab Comminity,
I am new to Matlab. I am working on an excercise which requires some binary operations, then write binary result in a file. Each character in the file must be 1 bit.
I tried looking for "binary datatype in Matlab", but it seems does not exist. I am little bit disappointed.
Could you guys give me some suggestions? What would you do when you want to work with binary number in Matlab?
Thank you so much!

답변 (2개)

Walter Roberson
Walter Roberson 2016년 5월 20일
fwrite(fileID, B, 'ubit1')
Caution: bits are only packed for any one call to fwrite(). Each time the data for an fwrite is finished outputting, if the bit stream is not aligned with a byte boundary then extra bits are written out to get to the byte boundary, leaving it positioned at a byte boundary for the next I/O call.

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 20일
you can work with logical data type
a=logical([1 0 0 1])
  댓글 수: 1
Tai Doan
Tai Doan 2016년 5월 20일
Thank you, Azzi! Is there any way to write them into a file as binary character? This is what I tried:
B = ones(1,1000);
B = logical(B);
fileID = fopen('binary', 'w');
fwrite(fileID,B);
fclose(fileID);
But the file's size is 1000 bytes, not 1000 bits. Any idea?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by