Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using empty array when indexing on DataStub object #605

Merged
merged 10 commits into from
Feb 11, 2025
24 changes: 23 additions & 1 deletion +types/+untyped/@DataStub/load_mat_style.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@
, iDimension, dimensionSize);
end

if isscalar(userSelection) && ~ischar(userSelection{1})
if isscalar(userSelection) && isempty(userSelection{1})
% If userselection (indices) is empty, get the first element of this
% DataStub and try to return an empty representation of that type.
data = obj.load_mat_style(1);
data = getEmptyRepresentation(data);
return

Check warning on line 42 in +types/+untyped/@DataStub/load_mat_style.m

View check run for this annotation

Codecov / codecov/patch

+types/+untyped/@DataStub/load_mat_style.m#L40-L42

Added lines #L40 - L42 were not covered by tests

elseif isscalar(userSelection) && ~ischar(userSelection{1})
% linear index into the fast dimension.
orderedSelection = unique(userSelection{1});

Expand Down Expand Up @@ -187,4 +194,19 @@
indexKeyIndex(indexKeyIndexNextIndex) = indexKeyIndex(indexKeyIndexNextIndex) + 1;
indexKeyIndex((indexKeyIndexNextIndex+1):end) = 1;
end
end


function emptyInstance = getEmptyRepresentation(nonEmptyInstance)
try
emptyInstance = nonEmptyInstance;
emptyInstance(:) = [];
catch ME
switch ME.identifier
case 'MATLAB:table:LinearSubscript'
emptyInstance(:, :) = [];

Check warning on line 207 in +types/+untyped/@DataStub/load_mat_style.m

View check run for this annotation

Codecov / codecov/patch

+types/+untyped/@DataStub/load_mat_style.m#L201-L207

Added lines #L201 - L207 were not covered by tests
otherwise
error('Not implemented for value of type: "%s"', class(nonEmptyInstance))

Check warning on line 209 in +types/+untyped/@DataStub/load_mat_style.m

View check run for this annotation

Codecov / codecov/patch

+types/+untyped/@DataStub/load_mat_style.m#L209

Added line #L209 was not covered by tests
end
end
end