Skip to content

Commit

Permalink
Tiny but essential corrections.
Browse files Browse the repository at this point in the history
  • Loading branch information
robdahn committed Jan 8, 2024
1 parent 8490d28 commit caeb8e2
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions cat_vol_ctype.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
if nargin==1 && ischar(Y) && strcmp(Y,'test'), testctype; return; end

types = {'int8','int16','int32','int64','single','float32','float64'...
'uint8','uint16','uint32','uint64','double','logical'};
'uint8','uint16','uint32','uint64','double'};

if ~exist('type','var')
type = 'uint8';
Expand All @@ -43,8 +43,8 @@
end
% use single for logical arrays to be compatible
type = cat_io_strrep(type, ...
{'logical', 'float32', 'float64'}, ...
{'single', 'single', 'double'});
{'float32', 'float64'}, ...
{'single', 'double'});


if iscell(Y)
Expand All @@ -56,23 +56,29 @@
type = types{contains(types, type)};

% prepare conversion
if contains({'int','char'}, type)
if contains('int', type)
switch class(Y)
case {'single','double'}
% replace nan
Y = single(Y);
Y(isnan(Y)) = 0;
Y = round( min( single(intmax(type)), max(single(intmin(type)), Y )));
case {'uint8','uint16'}
Y = min( uint16(intmax(type)), max(uint16(intmin(type)), uint16(Y) ));
case {'uint32','uint64'}
Y = min( uint32(intmax(type)), max(uint32(intmin(type)), uint32(Y) ));
case {'int8','int16'}
Y = min( int16(intmax(type)), max( int16(intmin(type)), uint16(Y) ));
case {'int32','int64'}
Y = min( int64(intmax(type)), max( int64(intmin(type)), uint64(Y) ));
otherwise
% this is not working for very old matlab versions
Y = int64(Y);
Y = round( min( int64(intmax(type)), max( int64(intmin(type)), Y )));
eval(sprintf('Y = min( double(intmax(''%s'')), max( double(intmin(''%s'')), double(Y) ))',type,type));
end
elseif contains(type,'single')
Y = eval([type '(Y)']);
Y = min( single(realmax(type)), max(single(realmin(type)), single(Y) ));
Y = min( single(realmax(type)), max(-single(realmax(type)), single(Y) ));
elseif contains(type,'double')
Y = min( double(realmax(type)), max(double(realmin(type)), double(Y) ));
Y = min( double(realmax(type)), max(-double(realmax(type)), double(Y) ));
end

% convert
Expand Down

0 comments on commit caeb8e2

Please sign in to comment.