Main Content

matlab::data::StructArray

C++ class to access MATLAB struct arrays

Description

Use StructArray objects to work with MATLAB® struct arrays. To access a field for a single element in the array, use the field name. To create a StructArray object, call createStructArray in the ArrayFactory class.

Class Details

Namespace:

matlab::data

Base class:

TypedArray<Struct>

Include:

StructArray.hpp

Constructors

Copy Constructors

StructArray(const StructArray& rhs)

StructArray(const Array& rhs)

Description

Creates a shared data copy of a StructArray object.

Parameters

const StructArray& rhs

Value to copy.

const Array& rhs

Value specified as ArrayType::STRUCT object.

Throws

matlab::data::InvalidArrayTypeException

Type of input Array is not ArrayType::STRUCT.

Copy Assignment Operators

StructArray& operator=(const StructArray& rhs)

StructArray& operator=(const Array& rhs)

Description

Assigns a shared data copy to a StructArray object.

Parameters

const StructArray& rhs

Value to copy.

const Array& rhs

Value specified as ArrayType::STRUCT object.

Returns

StructArray&

Updated instance.

Throws

matlab::data::InvalidArrayTypeException

Type of input Array is not ArrayType::STRUCT.

Move Constructors

StructArray(StructArray&& rhs)

StructArray(Array&& rhs)

Description

Moves contents of a StructArray object to a new instance.

Parameters

StructArray&& rhs

Value to move.

Array&& rhs

Value specified as ArrayType::STRUCT object.

Throws

matlab::data::InvalidArrayTypeException

Type of input Array is not ArrayType::STRUCT.

Move Assignment Operators

StructArray& operator=(StructArray&& rhs)

Description

Assigns the input to this StructArray object.

Parameters

StructArray&& rhs

Value to move.

Returns

StructArray&

Updated instance.

Throws

None

Destructor

~StructArray()

Description

Free memory for StructArray object.

Member Functions

getFieldNames

Range<ForwardIterator, MatlabFieldIdentifier const> getFieldNames() const
Returns

Range<ForwardIterator, MatlabFieldIdentifier const>

Contains begin and end iterators that enable access to all fields in StructArray object.

Throws

None

getNumberOfFields

size_t getNumberOfFields() const
Returns

size_t

Number of fields.

Throws

None

Examples

expand all

Assume that you have the following MATLAB structure.

s = struct('loc', {'east', 'west'}, 'data', {[1, 2, 3], [4., 5., 6., 7., 8.]})

Create a variable containing the data for loc east.

val = s(1).data

The following C++ code creates these variables.

#include "MatlabDataArray.hpp"

int main() {
	using namespace matlab::data;
	ArrayFactory factory;

	StructArray S = factory.createStructArray({ 1,2 }, { "loc", "data" });
	S[0]["loc"] = factory.createCharArray("east");
	S[0]["data"] = factory.createArray<uint8_t>({ 1, 3 }, { 1, 2, 3 });
	S[1]["loc"] = factory.createCharArray("west");
	S[1]["data"] = factory.createArray<double>({ 1, 5 }, { 4., 5., 6., 7., 8. });

	Reference<Array> val = S[0]["data"];
	return 0;
}

Version History

Introduced in R2017b