matlab::data::ArrayFactory - C++ class to create arrays - MATLAB (original) (raw)
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
Throws
matlab::data::FailedToLoadLibMatlabDataArrayException | Concrete implementation not loaded. |
---|
Destructor
~ArrayFactory()
Member Functions
- createArray
- createScalar
- createCellArray
- createCharArray
- createCharArrayFromUTF8
- createStructArray
- createEnumArray
- createSparseArray
- createEmptyArray
- createBuffer
- createArrayFromBuffer
createArray
template TypedArray createArray(ArrayDimensions dims)
template <typename ItType, typename T> TypedArray createArray(ArrayDimensions dims, ItType begin, ItType end, InputLayout inputLayout)
template TypedArray createArray(ArrayDimensions dims, const T* const begin, const T* const end)
template TypedArray createArray(ArrayDimensions dims, std::initializer_list data)
Description
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 theinputLayout
parameter.
Template Parameters
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 std::complex 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.
Parameters
ArrayDimensions dims | Dimensions for the array. |
---|---|
ItType beginItType end | Start and end of the user supplied data. Thevalue_type of the iterator determines the data type. |
InputLayout inputLayout | The layout of the input data. The values forInputLayout areCOLUMN_MAJOR orROW_MAJOR. |
const T* const beginconst T* const end | Start and end of the user supplied data specified as C-style pointer. This syntax supports all primitive types, complex types, and string types. |
std::initializer_list | Initializer list containing the data. |
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|---|
matlab::data::NumberOfElementsExceedsMaximumException | Number of elements is greater thansize_t. |
matlab::data::InvalidArrayTypeException | Input type of matlab::data::ObjectArray does not match the type of TypedArray. |
createScalar
template TypedArray createScalar(const T val)
TypedArray createScalar(const String val)
TypedArray createScalar(const std::string val)
ObjectArray createScalar(const Object& val);
Description
Creates a scalar TypedArray<T>
with the given value. This method supports arithmetic types, complex types, and string types.
Parameters
const T val | Value to be inserted into the scalar.For std::string parameters, if val is 7-bit ASCII data, then the method converts it to UTF16. |
---|---|
const String val | |
const std::string val | |
const Object& val |
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|---|
matlab::data::NonAsciiCharInInputDataException | Input is std::string and contains non-ASCII characters. |
Examples
#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)
Description
Creates a CellArray
with the specifieddata
. The data is in column-major order.
Template Parameters
...Targs | Variadic template of: arithmetic typecomplex typematlab::data::Stringstd::stringmatlab::data::Array |
---|
Parameters
ArrayDimensions dims | Dimensions of the cell array. |
---|---|
Targs... data | Elements to be inserted into the cell array, specified as a primitive type, complex type, string, orArray. |
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|---|
matlab::data::NonAsciiCharInInputDataException | Input is std::string and contains non-ASCII characters. |
matlab::data::NumberOfElementsExceedsMaximumException | Number of elements is greater thansize_t. |
Examples
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)
Description
Creates a 1xn CharArray
from the specified input, where n is the string length.
Parameters
matlab::data::String str | Data to be filled into the array. |
---|---|
std::string str |
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|---|
matlab::data::NonAsciiCharInInputDataException | Input is std::string and contains non-ASCII characters. |
Examples
#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)
Description
Creates a 1xn CharArray
from the specifiedstd::string
of UTF8 characters, where n is the string length.
Parameters
std::string str | Data to be filled into the array. |
---|
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|---|
matlab::data::InvalidUTF8InputException | Input string contains a non-UTF8 character. |
matlab::data::FeatureNotSupportedException | Code is running a version older than R2024b. |
Examples
#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::vectorstd::string fieldNames)
Description
Creates a StructArray
with the given dimensions and field names.
Parameters
ArrayDimensions dims | Dimensions for the array. |
---|---|
std::vectorstd::string fieldNames | Vector of the field names for the structure. |
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|---|
matlab::data::DuplicateFieldNameInStructArrayException | Duplicate field names specified. |
matlab::data::NumberOfElementsExceedsMaximumException | Number of elements is greater thansize_t. |
Examples
#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::vectorstd::string enums)
EnumArray createEnumArray(ArrayDimensions dims, std::string className)
Description
Creates an EnumArray
of type className
, which is a defined class. If specified, the method initializes the array with the list of enumeration names.
Parameters
ArrayDimensions dims | Dimensions for the array. |
---|---|
std::string className | Class name of the enumeration array. |
std::vectorstd::string enums | List of the enumeration names. |
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|---|
matlab::data::MustSpecifyClassNameException | Class name not specified. |
matlab::data::WrongNumberOfEnumsSuppliedException | Wrong number of enumerations provided. |
matlab::data::NumberOfElementsExceedsMaximumException | Number of elements is greater thansize_t. |
Examples
Create a matlab::data::EnumArray
object for theTextColor.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
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 SparseArray createSparseArray(ArrayDimensions dims, size_t nnz, buffer_ptr_t data, buffer_ptr_t rows, buffer_ptr_t cols)
Description
Creates a SparseArray<T>
withrows
-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.
Template Parameters
T | Element types, specified as double,bool, orstd::complex. |
---|
Parameters
ArrayDimensions dims | Dimensions for the array. |
---|---|
size_t nnz | Number of nonzero elements. |
buffer_ptr_t data | Buffer containing the nonzero elements. |
buffer_ptr_t<size_t> rows | Buffer containing the row value for each element. |
buffer_ptr_t<size_t> cols | Buffer containing the column value for each element. |
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|---|
matlab::data::InvalidDimensionsInSparseArrayException | More than two dimensions specified. |
matlab::data::NumberOfElementsExceedsMaximumException | Number of elements is greater thansize_t. |
Examples
#include "MatlabDataArray.hpp"
int main() { std::vector data = { 3.5, 12.98, 21.76 }; std::vector rows = { 0,0,1 }; std::vector 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
Descriptions
Creates an empty Array
containing no elements.
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|
createBuffer
template buffer_ptr_t createBuffer(size_t numberOfElements)
Description
Creates an uninitialized buffer to pass to thecreateArrayFromBuffer
method.
Parameters
size_t numberOfElements | Number of elements, not the actual buffer size. |
---|
Returns
buffer_ptr_t | Unique_ptr containing the buffer. |
---|
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|
createArrayFromBuffer
template TypedArray createArrayFromBuffer(ArrayDimensions dims, buffer_ptr_t buffer, MemoryLayout memoryLayout = MemoryLayout::COLUMN_MAJOR)
Description
Creates a TypedArray<T>
using the given buffer. You can specify a custom deleter function of typebuffer_deleter_t
to manage the buffer. (since R2024b)
Parameters
ArrayDimensions dims | Dimensions for the array. |
---|---|
buffer_ptr_t buffer | Buffer containing the data. The buffer is not copied. The TypedArray object takes ownership of the buffer. |
MemoryLayout memoryLayout | Memory layout for the input buffer and the created array, specified asMemoryLayout::COLUMN_MAJOR or asMemoryLayout::ROW_MAJOR. The default layout isCOLUMN_MAJOR.When usingmatlab::data::TypedIterator on an array created with createArrayFromBuffer,MemoryLayout affects the order of returned elements.This parameter is optional. |
Throws
matlab::OutOfMemoryException | Unable to allocate the array. |
---|---|
matlab::data::InvalidArrayTypeException | Buffer type not valid. |
matlab::data::InvalidMemoryLayoutException | Invalid memory layout. |
matlab::data::InvalidDimensionsInRowMajorArrayException | Dimensions not valid. This exception occurs for arrays created with MATLABĀ® R2019a and R2019b if a row-major array is not 2-D. |
matlab::data::NumberOfElementsExceedsMaximumException | Number of elements is greater thansize_t. |
Version History
Introduced in R2017b