簡體   English   中英

如何在Matlab中將位寫入文件

[英]How can i write bits to a file in matlab

我有一個看起來像這樣的表:

'00'

'010'

'011'

'100'

'1010'

'1011'

'1100'

'11010'

'11011'

'11100'

'11101'

'11110'

'11111'

這是一個包含一些字符的二進制編碼的單元格數組(基於Shannon-Fano算法。我的問題是我如何將此代碼寫入文件,以便每個0和1都被解釋為一點。

fwrite(F,V{I,3},'bit1')可以工作(二進制編碼在第三列上,並使用I標識行)?

不,文件操作本質上是面向字節的。 您可能無法寫出部分字節。 您需要將所有位連接到一個字節字符串中,並將該字符串寫出。 該代碼可能看起來像這樣:

allbits = cat(2, V{:,3});   % concatenate all bits into one giant binary string
npadding = 8 - mod(length(allbits), 8); % number of bits needed to produce an even multiple of 8
if(npadding < 8)   % pad with zeros
    allbits = [allbits repmat('0', 1, npadding)];
end
bytestring = reshape(allbits, 8, []).';  % reshape into a matrix of binary strings
bytes = bin2dec(bytestring);  % convert to integers
fwrite(fid, bytes, 'uint8');   % be sure to write out the integers as 8-bit bytes

這段代碼做出了一些假設,您需要根據自己的期望進行調整:文件中的位順序,不完整字節的填充類型等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM