matlab如何判断大小,如何知道在MATLAB中的變量的大小

我試圖提升「MatlabSorter的」簡單的函數來處理這個問題。用法仍然是相同的:

>> x = ones(1000);

>> getByteSize(x)

7.63 mb

補充:

1.you能說出你尋求哪種類型的回報 - B,KB,MB,TB或PB

2.you可以得到結果作爲一個變量,但不打印屏幕

這裏上的代碼:

function b = getByteSize(theVariable, returnType, fid)

% getByteSize returns the mem.usage of the provided variable(theVariable) to the given file

% identifier.

% returnType is assigned meaningfully according to the byte size if not stated

% Output is written to screen if fid is 1, empty or not provided.

s = whos('theVariable');

b = s.bytes;

if nargin == 1 || isempty(returnType)

scale = floor(log(b)/log(1024));

switch scale

case 0

returnType = 'byte';

case 1

returnType = 'kb';

case 2

returnType = 'mb';

case 3

returnType = 'gb';

case 4

returnType = 'tb';

case -inf

% Size occasionally returned as zero (eg some Java objects).

returnType = 'byte';

warning('Size occasionally returned as zero (eg some Java objects). Bytes assumed');

otherwise

returnType = 'petabytes';

warning('Over 1024 petabyte. petabytes assumed');

end

end

switch returnType

case {'b','byte','bytes'}

b = s.bytes;

case {'kb','kbs','kilobyte','kilobytes'}

b = b/1024;

case {'mb','mbs','megabyte','megabytes'}

b = b/1024^2;

case {'gb','gbs','gigabyte','gigabytes'}

b = b/1024^3;

case {'tb','tbs','terabyte','terabytes'}

b = b/1024^4;

case {'pb','pbs','petabyte','petabytes'}

b = b/1024^5;

otherwise

returnType = 'bytes';

end

if nargin <= 2 || isempty(fid) || fid == 1

fprintf(1,[num2str(b) ' ' returnType '\n']);

elseif nargin > 2 && ~isempty(fid) && fid > 2

try

fprintf(fid,[num2str(b) ' ' returnType '\n']);

catch

warning(['fid(' num2str(fid) ') could not be edited. Hence the output will be written on the screen.']);

fprintf(1,[num2str(b) ' ' returnType '\n']);

end

end

end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值