Hauptinhalt

MathWorks.MATLAB.Types.MATLABStruct

.NET class to represent scalar MATLAB structures

Since R2023a

Description

The MathWorks.MATLAB.Types.MATLABStruct type represents the MATLAB® struct type. The object behaves like a read-only dictionary.

Class Details

Namespace:

MathWorks.MATLAB.Types
Superclass:System.Collections.Generic.IReadOnlyDictionary

Creation

MATLABStruct(params(string,object)[] args) creates an instance of MATLABStruct from a list of keys and values. The args should be in the form key, value. The string must be a valid MATLAB identifier.

Methods

Public Methods

bool IsField(String field)

Determine if the struct contains this field.

dynamic GetField(String field)

The value associated with the field.

dynamic x = s.[propertyName];

The value associated with property name propertyName. This is invoked via dot notation.

override bool Equals(object o)

Compare the specified object with the MATLABStruct for equality. Equality is defined based on reference not value.

override int GetHashCode()

The hash code value for this MATLABStruct.

int Count

The number of key-value pairs in the struct.

System.Collections.Generic.IEnumerable<String> GetFieldNames

The fields in the struct.

Examples

expand all

Create a MATLABStruct object in C# and assign a key and value. For information about creating this C# example, see StructExample.cs in Test Your .NET Development Environment.

Create a MATLABStruct object and assign it to the dynamic type.

MATLABStruct mStruct = new MATLABStruct(("a", 1), ("b", 2));
dynamic dynamicMStruct = mStruct;

Access field a using the GetField method.

double a = mStruct.GetField("a");

Access field b directly from the MATLABStruct object.

double b = dynamicMStruct.b;

Call MATLABStruct methods.

int numFields = mStruct.Count();
bool containsFieldA = mStruct.IsField("a");
IEnumerable<string> fieldNames = mStruct.GetFieldNames();

Exceptions

FieldNotFoundException

Field is not defined in the struct.

UnsupportedTypeException

Passing an object that is not an anonymous type when expecting the anonymous type.

ArgumentException

Field name is not a valid MATLAB identifier.

Version History

Introduced in R2023a