필터 지우기
필터 지우기

Does Matlab have a function which is similar with sizeof in C?

조회 수: 33 (최근 30일)
Linan Xu
Linan Xu 2018년 3월 28일
편집: Jan 2021년 4월 13일
For example, if I type sizeof('double'), the function can give me 8. Let me know whether Matlab has a function working similarly with sizeof in C language.
  댓글 수: 2
Steven Lord
Steven Lord 2018년 3월 28일
How would you use such a function if it existed? You don't need to allocate memory yourself like you do in C, instead you should use functions like zeros, ones, etc. to preallocate your arrays.
mysiak
mysiak 2019년 11월 8일
For example when reading custom binary files.
I created simple binary file where all elements are of say 64bit long int64. Now I want to read such binary file in matlab with fread. I want to use fseek in order to navigate through the file, but I need to move in chunks of data which correspond to meaningful frame in my applicatio, say 5 numbers of int64.
So the fseek looks like fseek(fileid, 5*8, 'cof');
Then I use fread like fread(fileID,5,'int64')
Now it would be good if the 5*8 in fseek could be replaced by 5 *sizeof('int64') since matlab is aware of the int64 type. So the number 8 should be tied to int64 type.

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

답변 (3개)

Birdman
Birdman 2018년 3월 28일
Use the sizeof.m file from the following link:
Type:
sizeof('double')

Jan
Jan 2018년 3월 28일
편집: Jan 2021년 4월 13일
A cheap method would be:
function S = sizeof(V)
switch lower(V)
case {'double', 'int64', 'uint64'}
S = 8;
case {'single', 'int32', 'uint32'}
S = 4;
case {'char', 'int16', 'uint16'}
S = 2;
case {'logical', 'int8', 'uint8'}
S = 1;
otherwise
warning('Jan:sizeof:BadClass', 'Class "%s" is not supported.', V);
S = NaN;
end
end

Horshack Horshack
Horshack Horshack 2021년 4월 10일
편집: Horshack Horshack 2021년 4월 12일
Edit: Oops, this doesn't work in Matlab, only Octave.
I simply cast an arbitrary constant value (zero) to the type and then take the size of the lvalue result. Examples:
sizeof(uint8(0))
sizeof(uint16(0))
sizeof(uint32(0))
sizeof(double(0))
Output:
1
2
4
8

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by