matlab::data::ArrayFactory
C++ class to create arrays
Description
Use ArrayFactory
to create matlab::data::Array
objects.
Class Details
Namespace: | matlab::data |
Include: | ArrayFactory.hpp |
Constructors
Default Constructor
ArrayFactory()
|
Concrete implementation not loaded. |
Destructor
~ArrayFactory()
Member Functions
createArray
template <typename T> TypedArray<T> createArray(ArrayDimensions dims)
template <typename ItType, typename T> TypedArray<T> createArray(ArrayDimensions dims, ItType begin, ItType end, InputLayout inputLayout)
template <typename T> TypedArray<T> createArray(ArrayDimensions dims, const T* const begin, const T* const end)
template <typename T> TypedArray<T> createArray(ArrayDimensions dims, std::initializer_list<T> data)
Creates a TypedArray<T>
with the given dimensions. If
specified, createArray
fills the array with data. The data is
copied by default in column-major order. To specify the data layout, use the
inputLayout
parameter.
ItType
- Iterator types, specified asstd::iterator
.T
- Element types, specified as one of the following C++ data types.bool
int8_t
int16_t
int32_t
int64_t
uint8_t
uint16_t
uint32_t
uint64_t
float
double
char16_t
matlab::data::String
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::MATLABString
To create an array of
matlab::data::Object
element types, use theTypedArray<T> createArray(ArrayDimensions dims, ItType begin, ItType end)
syntax.
| Dimensions for the array. |
| Start and end of the user supplied data. The
|
| The layout of the input data. The values for
|
| Start and end of the user supplied data specified as C-style pointer. This syntax supports all primitive types, complex types, and string types. |
| Initializer list containing the data. |
| Unable to allocate the array. |
| Number of elements is greater than
|
| Input type of |
createScalar
template <typename T> TypedArray<T> createScalar(const T val)
TypedArray<MATLABString> createScalar(const String val)
TypedArray<MATLABString> createScalar(const std::string val)
ObjectArray createScalar(const Object& val);
Creates a scalar TypedArray<T>
with the given value. This
method supports arithmetic types, complex types, and string types.
| Value to be inserted into the scalar. For |
| |
| |
|
| Unable to allocate the array. |
| Input is |
#include "MatlabDataArray.hpp" int main() { matlab::data::ArrayFactory factory; // Create a vector containing two scalar values std::vector<matlab::data::Array> args({ factory.createScalar<int16_t>(100), factory.createScalar<int16_t>(60)}); return 0; }
createCellArray
CellArray createCellArray(ArrayDimensions dims)
template <typename ...Targs> CellArray createCellArray(ArrayDimensions dims, Targs... data)
Creates a CellArray
with the specified
data
. The data is in column-major order.
|
Variadic template of:
|
|
Dimensions of the cell array. |
| Elements to be inserted into the cell array, specified
as a primitive type, complex type, string, or
|
|
Unable to allocate the array. |
|
Input is |
| Number of elements is greater than
|
Create a two element cell array containing an std::string
and a double.
#include "MatlabDataArray.hpp" int main() { using namespace matlab::data; ArrayFactory f; CellArray myArray = f.createCellArray({ 1,2 }, std::string("MATLAB Cell Array"), 5.5); return 0; }
createCharArray
CharArray createCharArray(String str)
CharArray createCharArray(std::string str)
Creates a 1xn CharArray
from the specified input, where n is
the string length.
|
Data to be filled into the array. |
|
|
Unable to allocate the array. |
|
Input is |
#include "MatlabDataArray.hpp" int main() { using namespace matlab::data; ArrayFactory factory; CharArray A = factory.createCharArray("This is a char array"); return 0; }
createCharArrayFromUTF8
CharArray createCharArray(const std::string& str)
Creates a 1xn CharArray
from the specified
std::string
of UTF8 characters, where n is the string
length.
| Data to be filled into the array. |
| Unable to allocate the array. |
| Input string contains a non-UTF8 character. |
| Code is running a version older than R2024b. |
#include "MatlabDataArray.hpp" int main() { using namespace matlab::data; ArrayFactory factory; // "UTF8 string" std::string utf8str = "\x55\x54\x46\x20\x73\x74\x72\x69\x6e\x67"; CharArray A = factory.createCharArrayFromUTF8(utf8str); return 0; }
createStructArray
StructArray createStructArray(ArrayDimensions dims, std::vector<std::string> fieldNames)
Creates a StructArray
with the given dimensions and field
names.
|
Dimensions for the array. |
|
Vector of the field names for the structure. |
|
Unable to allocate the array. |
|
Duplicate field names specified. |
| Number of elements is greater than
|
#include "MatlabDataArray.hpp" int main() { using namespace matlab::data; ArrayFactory f; // Create StructArray equivalent to MATLAB structure s: // s = struct('loc', {'east', 'west'}, 'data', {[1, 2, 3], [4., 5., 6., 7., 8.]}) StructArray S = f.createStructArray({ 1,2 }, { "loc", "data" }); S[0]["loc"] = f.createCharArray("east"); S[0]["data"] = f.createArray<uint8_t>({ 1, 3 }, { 1, 2, 3 }); S[1]["loc"] = f.createCharArray("west"); S[1]["data"] = f.createArray<double>({ 1, 5 }, { 4., 5., 6., 7., 8. }); // Access the value defined by the MATLAB statement: // s(1).data Reference<Array> val = S[0]["data"]; return 0; }
createEnumArray
EnumArray createEnumArray(ArrayDimensions dims, std::string className, std::vector<std::string> enums)
EnumArray createEnumArray(ArrayDimensions dims, std::string className)
Creates an EnumArray
of type className
,
which is a defined class. If specified, the method initializes the array with
the list of enumeration names.
|
Dimensions for the array. |
|
Class name of the enumeration array. |
|
List of the enumeration names. |
|
Unable to allocate the array. |
|
Class name not specified. |
|
Wrong number of enumerations provided. |
| Number of elements is greater than
|
Create a matlab::data::EnumArray
object for the
TextColor.Blue
enumeration argument defined in this
class.
classdef TextColor enumeration Red Green Blue end end
Move the value into an argument vector.
#include "MatlabDataArray.hpp" #include <vector> int main() { using namespace matlab::data; ArrayFactory f; auto blue = f.createEnumArray({ 1,1 }, "TextColor", { "Blue" }); // Create an argument vector std::vector<Array> args({ f.createCharArray("My text"), std::move(blue) }); return 0; }
For more examples, see Pass Enumerations to MATLAB from C++.
createSparseArray
template <typename T> SparseArray<T> createSparseArray(ArrayDimensions dims, size_t nnz, buffer_ptr_t<T> data, buffer_ptr_t<size_t> rows, buffer_ptr_t<size_t> cols)
Creates a SparseArray<T>
with
rows
-by-cols
dimensions. You can only
have two dimensions for sparse arrays. The method does not copy the buffer and
the array takes ownership of the memory.
|
Element types, specified as |
|
Dimensions for the array. |
|
Number of nonzero elements. |
|
Buffer containing the nonzero elements. |
|
Buffer containing the row value for each element. |
|
Buffer containing the column value for each element. |
|
Unable to allocate the array. |
|
More than two dimensions specified. |
| Number of elements is greater than
|
#include "MatlabDataArray.hpp" int main() { std::vector<double> data = { 3.5, 12.98, 21.76 }; std::vector<size_t> rows = { 0,0,1 }; std::vector<size_t> cols = { 0,4,8 }; size_t nnz = 3; matlab::data::ArrayFactory factory; auto data_p = factory.createBuffer<double>(nnz); auto rows_p = factory.createBuffer<size_t>(nnz); auto cols_p = factory.createBuffer<size_t>(nnz); double* dataPtr = data_p.get(); size_t* rowsPtr = rows_p.get(); size_t* colsPtr = cols_p.get(); std::for_each(data.begin(), data.end(), [&](const double& e) { *(dataPtr++) = e; }); std::for_each(rows.begin(), rows.end(), [&](const size_t& e) { *(rowsPtr++) = e; }); std::for_each(cols.begin(), cols.end(), [&](const size_t& e) { *(colsPtr++) = e; }); matlab::data::SparseArray<double> arr = factory.createSparseArray<double>({ 2,9 }, nnz, std::move(data_p), std::move(rows_p), std::move(cols_p)); return 0; }
createEmptyArray
Array createEmptyArray()
Creates an empty Array
containing no elements.
|
Empty array. |
|
Unable to allocate the array. |
createBuffer
template <typename T> buffer_ptr_t<T> createBuffer(size_t numberOfElements)
Creates an uninitialized buffer to pass to the
createArrayFromBuffer
method.
|
Primitive types. |
|
Number of elements, not the actual buffer size. |
|
Unique_ptr containing the buffer. |
|
Unable to allocate the array. |
createArrayFromBuffer
template <typename T> TypedArray<T> createArrayFromBuffer(ArrayDimensions dims, buffer_ptr_t<T> buffer, MemoryLayout memoryLayout = MemoryLayout::COLUMN_MAJOR)
Creates a TypedArray<T>
using the given buffer. You can specify a custom deleter function of type
buffer_deleter_t
to manage the
buffer. (since R2024b)
|
Primitive types. |
|
Dimensions for the array. |
| Buffer containing the data. The buffer is not copied.
The |
| Memory layout for the input buffer and the created
array, specified as
When using
This parameter is optional. |
| Unable to allocate the array. |
| Buffer type not valid. |
| Invalid memory layout. |
| Dimensions not valid. This exception occurs for arrays created with MATLAB® R2019a and R2019b if a row-major array is not 2-D. |
| Number of elements is greater than
|