matlab::data::TypedArray - Templated C++ class to access array data - MATLAB (original) (raw)
Main Content
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
, callcreateArray or createScalar in theArrayFactory
class with one of the templates listed in Template Instantiations.
This class defines the following iterator types:
using iterator = TypedIterator; using const_iterator = TypedIterator;
Class Details
Template Parameters
T | Type of element referred to. |
---|
Template Instantiations
Constructors
Copy Constructor
TypedArray(const TypedArray<T>& rhs)
TypedArray(const Array& rhs)
Description
Creates a shared data copy of the input.
Parameters
const TypedArray& 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. |
---|
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 thisTypedArray<T>
.
Parameters
const TypedArray& rhs | Value to be copied. |
---|---|
const Array& rhs | Value specified as matlab::data::Array object. |
Returns
TypedArray& | Updated instance. |
---|
Throws
matlab::data::InvalidArrayTypeException | Type of input Array does not match the type for TypedArray. |
---|
Move Constructor
TypedArray(TypedArray<T>&& rhs)
Description
Moves contents of the input to a new instance.
Parameters
TypedArray&& 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&& rhs | Value to move. |
---|
Returns
TypedArray& | Updated instance. |
---|
Throws
matlab::data::InvalidArrayTypeException | Type of input Array does not match the type for TypedArray. |
---|
Destructor
virtual ~TypedArray()
Iterators
Begin Iterators
const_iterator begin() const
const_iterator cbegin() const
Returns
iterator | Iterator to beginning of array, specified asTypedIterator. |
---|---|
const_iterator | Iterator, specified as TypedIterator<typename std::add_const::type>. |
End Iterators
const_iterator end() const
const_iterator cend() const
Returns
iterator | Iterator to end of array, specified asTypedIterator. |
---|---|
const_iterator | Iterator, specified as TypedIterator<typename std::add_const::type>. |
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::value> | Temporary object containing index specified. If typeT 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. |
Member Functions
release
buffer_ptr_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 | unique_ptr containing data pointer. |
---|
Throws
matlab::data::InvalidArrayTypeException | TypedArray does not support releasing the buffer. |
---|
Examples
Assign Values to Array Elements
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 D = factory.createArray({ 2,2 }, { 1,3,2,4 }); // Change the values. for (auto& elem : D) { elem = 5.5; } return 0; }
Version History
Introduced in R2017b