Skip to content

Commit

Permalink
fix for type correction.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancewiu committed Jan 22, 2019
1 parent 53f9cbf commit 11464ae
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 0 additions & 1 deletion +types/+util/checkDtype.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
try
val = types.util.correctType(val, type);
catch ME
keyboard;
error('MATNWB:CASTERROR', 'Could not cast type `%s` to `%s` for property `%s`',...
class(val), type, name);
end
Expand Down
10 changes: 6 additions & 4 deletions +types/+util/correctType.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
%CORRECTTYPE upcasts if type is smaller than minimum
% Will error if type is simply incompatible
% Will throw if casting is impossible

if nargin < 3
allowDowncast = false;
end
%check different types and correct

if startsWith(type, 'float') && ~isfloat(val)
Expand All @@ -18,8 +20,8 @@
end

%check different types sizes and upcast to meet minimum (if applicable)
if any(strcmp(type, {'float64' 'float32'})
if issingle(val)
if any(strcmp(type, {'float64' 'float32'}))
if isa(val, 'single')
val = double(val);
elseif allowDowncast && strcmp(type, 'float32')
val = single(val);
Expand All @@ -33,7 +35,7 @@
typsz = sscanf(type, pattern);
valsz = sscanf(class(val), pattern);

if valsz < typsz || (nargin > 2 && allowDowncast)
if valsz < typsz || allowDowncast
val = eval([type '(val)']);
end
end
Expand Down

0 comments on commit 11464ae

Please sign in to comment.