How can I use function 'typecast' to convert triplets of 8-byte variables to int24?

조회 수: 9 (최근 30일)
It is provided triplets of 8 bytes which are sought to be converted to 'int24' datatype. Consider an array 'A' that contains such kind of triplets, namely,
 
A = uint8([1 2 3 4 5 6]);
 
How can I use function 'typecast' to convert triplets of a 8 bytes, namely 'A(1:3)' and 'A(4:6)' in this case, to 'int24' datatype?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2021년 7월 27일
It is not possible to use function 'typecast' to convert a variable to 'int24', see the following documentation page accordingly,
However, one can add 0 padding into the corresponding triplets and have them converted to 'int32' using function 'typecast' which is the next higher datatype to datatype 'int24'. This workflow is demonstrated using the following code snippet,
% Raw example data
A = uint8([1 2 3 4 5 6]);
% Reshape such that we can then easily add a row of zeros to all of them
A = reshape(A,3,[]);
% Add the padding zeros
A = [A;zeros(1,size(A,2))];
% One typecast for the whole flat array again
typecast(A(:),'uint32')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by