Main Content

matlab::data::TypedArray<T>

Templated C++ class to access array data

Description

The templated TypedArray class provides type-safe APIs to handle all MATLAB array types (except sparse arrays). To create a TypedArray, call createArray or createScalar in the ArrayFactory class with one of the templates listed in Template Instantiations.

This class defines the following iterator types:

using iterator = TypedIterator<T>;
using const_iterator = TypedIterator<T const>;

Class Details

Namespace:

matlab::data

Base class:

matlab::data::Array

Include:

TypedArray.hpp

Template Parameters

T

Type of element referred to.

Template Instantiations

double

float

int8_t

uint8_t

int16_t

uint16_t

int32_t

uint32_t

int64_t

uint64_t

char16_t

bool

std::complex<double>

std::complex<float>

std::complex<int8_t>

std::complex<uint8_t>

std::complex<int16_t>

std::complex<uint16_t>

std::complex<int32_t>

std::complex<uint32_t>

std::complex<int64_t>

std::complex<uint64_t>

matlab::data::Array
matlab::data::Struct
matlab::data::Enumeration

matlab::data::MATLABString

Constructors

Copy Constructor

TypedArray(const TypedArray<T>& rhs)

TypedArray(const Array& rhs)

Description

Creates a shared data copy of the input.

Parameters

const TypedArray<T>& rhs

Value to be copied.

const Array& rhs

Value specified as matlab::data::Array object.

Throws

matlab::data::InvalidArrayTypeException

Type of input Array does not match the type for TypedArray<T>.

Copy Assignment Operator

TypedArray<T>& operator=(const TypedArray<T>& rhs)

TypedArray<T>& operator=(const Array& rhs)

Description

Assigns a shared data copy of the input to this TypedArray<T>.

Parameters

const TypedArray<T>& rhs

Value to be copied.

const Array& rhs

Value specified as matlab::data::Array object.

Returns

TypedArray<T>&

Updated instance.

Throws

matlab::data::InvalidArrayTypeException

Type of input Array does not match the type for TypedArray<T>.

Move Constructor

TypedArray(TypedArray<T>&& rhs)

TypedArray(Array&& rhs)

Description

Moves contents of the input to a new instance.

Parameters

TypedArray<T>&& rhs

Value to be moved.

Array&& rhs

Value specified as matlab::data::Array object.

Throws

matlab::data::InvalidArrayTypeException

Type of input does not match.

Move Assignment Operator

TypedArray<T>& operator=(TypedArray<T>&& rhs)

TypedArray<T>& operator=(Array&& rhs)

Description

Moves the input to this TypedArray<T> object.

Parameters

TypedArray<T>&& rhs

Value to move.

Returns

TypedArray<T>&

Updated instance.

Throws

matlab::data::InvalidArrayTypeException

Type of input Array does not match the type for TypedArray<T>.

Destructor

virtual ~TypedArray()

Iterators

Begin Iterators

iterator begin()

const_iterator begin() const

const_iterator cbegin() const

Returns

iterator

Iterator to beginning of array, specified as TypedIterator<T>.

const_iterator

Iterator, specified as TypedIterator<typename std::add_const<T>::type>.

Throws

None

End Iterators

iterator end()

const_iterator end() const

const_iterator cend() const

Returns

iterator

Iterator to end of array, specified as TypedIterator<T>.

const_iterator

Iterator, specified as TypedIterator<typename std::add_const<T>::type>.

Throws

None

Indexing Operators

operator[]

ArrayElementTypedRef<T, std::is_const<T>::value> operator[](size_t idx)

ArrayElementTypedRef<T, true> operator[](size_t idx) const

Description

Enables [] indexing on a TypedArray. Indexing is 0-based.

Parameters

size_t idx

First array index.

Returns

ArrayElementTypedRef<T, std::is_const<T>::value>

Temporary object containing index specified. If type T is const, then the return value allows the element of the array to be retrieved, but not modified. Otherwise, the element can be modified or retrieved.

ArrayElementTypedRef<T, true>

Temporary object containing index specified. The return value allows the element of the array to be retrieved, but not modified.

Throws

None

Member Functions

release

buffer_ptr_t<T> release()
Description

Release the underlying buffer from the Array.

  • If the Array has shared copies, the function unshares the Array from the copies. The buffer of the unshared Array contains the copied data and the default deleter. (since R2024b) Then the function releases the buffer.

  • If the Array does not have shared copies, the function releases the buffer.

After releasing the buffer, the Array contains no elements. For more information about sharing and unsharing copies of Array, see Copy C++ MATLAB Data Arrays.

Returns

buffer_ptr_t<T>

unique_ptr containing data pointer.

Throws

matlab::data::InvalidArrayTypeException

TypedArray does not support releasing the buffer.

Examples

expand all

Create an array equivalent to the MATLAB array [1 2; 3 4], then replace each element of the array with a single value.

#include "MatlabDataArray.hpp"

int main() {
	matlab::data::ArrayFactory factory;
	// Create an array equivalent to the MATLAB array [1 2; 3 4].
	matlab::data::TypedArray<double> D = factory.createArray<double>({ 2,2 }, { 1,3,2,4 });
	// Change the values.
	for (auto& elem : D) {
		elem = 5.5;
	}
	return 0;
}

Version History

Introduced in R2017b