diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/array.cc b/matlab/src/cpp/arrow/matlab/array/proxy/array.cc index 865dd694b78..038eeb701c1 100644 --- a/matlab/src/cpp/arrow/matlab/array/proxy/array.cc +++ b/matlab/src/cpp/arrow/matlab/array/proxy/array.cc @@ -24,7 +24,7 @@ namespace arrow::matlab::array::proxy { // Register Proxy methods. REGISTER_METHOD(Array, ToString); REGISTER_METHOD(Array, ToMatlab); - + REGISTER_METHOD(Array, Length); } void Array::ToString(libmexclass::proxy::method::Context& context) { @@ -34,4 +34,10 @@ namespace arrow::matlab::array::proxy { auto str_mda = factory.createScalar(array->ToString()); context.outputs[0] = str_mda; } + + void Array::Length(libmexclass::proxy::method::Context& context) { + ::matlab::data::ArrayFactory factory; + auto length_mda = factory.createScalar(array->length()); + context.outputs[0] = length_mda; + } } diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/array.h b/matlab/src/cpp/arrow/matlab/array/proxy/array.h index 8a166303487..0ca75bcce47 100644 --- a/matlab/src/cpp/arrow/matlab/array/proxy/array.h +++ b/matlab/src/cpp/arrow/matlab/array/proxy/array.h @@ -33,6 +33,8 @@ class Array : public libmexclass::proxy::Proxy { void ToString(libmexclass::proxy::method::Context& context); + void Length(libmexclass::proxy::method::Context& context); + virtual void ToMatlab(libmexclass::proxy::method::Context& context) = 0; std::shared_ptr array; diff --git a/matlab/src/matlab/+arrow/+array/Array.m b/matlab/src/matlab/+arrow/+array/Array.m new file mode 100644 index 00000000000..259dba0cea0 --- /dev/null +++ b/matlab/src/matlab/+arrow/+array/Array.m @@ -0,0 +1,51 @@ +classdef (Abstract) Array < matlab.mixin.CustomDisplay & ... + matlab.mixin.Scalar + % arrow.array.Array + + % Licensed to the Apache Software Foundation (ASF) under one or more + % contributor license agreements. See the NOTICE file distributed with + % this work for additional information regarding copyright ownership. + % The ASF licenses this file to you under the Apache License, Version + % 2.0 (the "License"); you may not use this file except in compliance + % with the License. You may obtain a copy of the License at + % + % http://www.apache.org/licenses/LICENSE-2.0 + % + % Unless required by applicable law or agreed to in writing, software + % distributed under the License is distributed on an "AS IS" BASIS, + % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + % implied. See the License for the specific language governing + % permissions and limitations under the License. + + + properties (Access=protected) + Proxy + end + + properties (Dependent) + Length + end + + methods + function obj = Array(varargin) + obj.Proxy = libmexclass.proxy.Proxy(varargin{:}); + end + + function numElements = get.Length(obj) + numElements = obj.Proxy.Length(); + end + end + + methods (Access = private) + function str = ToString(obj) + str = obj.Proxy.ToString(); + end + end + + methods (Access=protected) + function displayScalarObject(obj) + disp(obj.ToString()); + end + end +end + diff --git a/matlab/src/matlab/+arrow/+array/Float64Array.m b/matlab/src/matlab/+arrow/+array/Float64Array.m index c48ee455746..ec77ef72216 100644 --- a/matlab/src/matlab/+arrow/+array/Float64Array.m +++ b/matlab/src/matlab/+arrow/+array/Float64Array.m @@ -1,4 +1,4 @@ -classdef Float64Array < matlab.mixin.CustomDisplay +classdef Float64Array < arrow.array.Array % arrow.array.Float64Array % Licensed to the Apache Software Foundation (ASF) under one or more @@ -16,10 +16,6 @@ % implied. See the License for the specific language governing % permissions and limitations under the License. - properties (Access=private) - Proxy - end - properties (Hidden, SetAccess=private) MatlabArray end @@ -33,25 +29,13 @@ validateattributes(data, "double", ["2d", "nonsparse", "real"]); if ~isempty(data), validateattributes(data, "double", "vector"); end + obj@arrow.array.Array("Name", "arrow.array.proxy.Float64Array", "ConstructorArguments", {data, opts.DeepCopy}); % Store a reference to the array if not doing a deep copy if (~opts.DeepCopy), obj.MatlabArray = data; end - obj.Proxy = libmexclass.proxy.Proxy("Name", "arrow.array.proxy.Float64Array", "ConstructorArguments", {data, opts.DeepCopy}); end function data = double(obj) data = obj.Proxy.ToMatlab(); end end - - methods (Access=protected) - function displayScalarObject(obj) - disp(obj.ToString()); - end - end - - methods (Access=private) - function str = ToString(obj) - str = obj.Proxy.ToString(); - end - end end diff --git a/matlab/test/arrow/array/tFloat64Array.m b/matlab/test/arrow/array/tFloat64Array.m index 7e1a878c425..f85feb78e75 100755 --- a/matlab/test/arrow/array/tFloat64Array.m +++ b/matlab/test/arrow/array/tFloat64Array.m @@ -100,5 +100,22 @@ function ErrorIfSparse(testCase, MakeDeepCopy) fcn = @() arrow.array.Float64Array(sparse(ones([10 1])), DeepCopy=MakeDeepCopy); testCase.verifyError(fcn, "MATLAB:expectedNonsparse"); end + + function Length(testCase, MakeDeepCopy) + % Zero length array + A = arrow.array.Float64Array([], DeepCopy=MakeDeepCopy); + expectedLength = int64(0); + testCase.verifyEqual(A.Length, expectedLength); + + % Scalar + A = arrow.array.Float64Array(1, DeepCopy=MakeDeepCopy); + expectedLength = int64(1); + testCase.verifyEqual(A.Length, expectedLength); + + % Vector + A = arrow.array.Float64Array(1:100, DeepCopy=MakeDeepCopy); + expectedLength = int64(100); + testCase.verifyEqual(A.Length, expectedLength); + end end end