Skip to content

Commit

Permalink
Add support for converting a ChunkedArray of StructArrays to a MATLAB…
Browse files Browse the repository at this point in the history
… table
  • Loading branch information
sgilmore10 committed Sep 20, 2023
1 parent d67e20f commit 9e8809a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
3 changes: 2 additions & 1 deletion matlab/src/matlab/+arrow/+array/ChunkedArray.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
for ii = 1:obj.NumChunks
chunk = obj.chunk(ii);
endIndex = startIndex + chunk.Length - 1;
data(startIndex:endIndex) = toMATLAB(chunk);
% Use 2D indexing to support tabular MATLAB types.
data(startIndex:endIndex, :) = toMATLAB(chunk);
startIndex = endIndex + 1;
end
end
Expand Down
32 changes: 23 additions & 9 deletions matlab/src/matlab/+arrow/+type/StructType.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,28 @@
end

methods (Hidden)
% TODO: Consider using a mixin approach to add this behavior. For
% example, ChunkedArray's toMATLAB method could check if its
% Type inherits from a mixin called "Preallocateable" (or something
% more descriptive). If so, we can call preallocateMATLABArray
% in the toMATLAB method.
function preallocateMATLABArray(~)
error("arrow:type:UnsupportedFunction", ...
"preallocateMATLABArray is not supported for StructType");
end
function data = preallocateMATLABArray(obj, numElements)
import arrow.tabular.internal.*

fields = obj.Fields;

% Construct the VariableNames and VariableDimensionNames
fieldNames = [fields.Name];
validVariableNames = makeValidVariableNames(fieldNames);
validDimensionNames = makeValidDimensionNames(validVariableNames);

% Preallocates each table variable. Uses the child field types
% to construct the correct MATLAB type.
variableData = cell(1, numel(fields));
for ii = 1:numel(fields)
type = fields(ii).Type;
variableData{ii} = preallocateMATLABArray(type, numElements);
end

% Return a table with the appropriate schema and dimensions
data = table(variableData{:}, ...
VariableNames=validVariableNames, ...
DimensionNames=validDimensionNames);
end
end
end

0 comments on commit 9e8809a

Please sign in to comment.