Main Content

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 in C# and assign a key and value.

using MathWorks.MATLAB.Types;
using MathWorks.MATLAB.Engine;
 
namespace CircleExample{
    class Main{
 
        public static void Creation(dynamic eng){
            //Creation
            MATLABStruct circle1 = new MATLABStruct(("r",3),("piVal",3));
             
            //Code shown in figure three in requirements
            eng.getCircleArea(circle1);
        }
 
        public static void FunctionCall(dynamic eng){
            //Call a function that returns a MATLAB struct
            double radius= 5;
 
            //Code shown in figure four in requirements
            MATLABStruct circle2 = eng.circleInfo(radius);  
        }
 
        public static void main(string[] args){
            dynamic eng = MATLABEngine.StartMATLAB();
            eng.Dispose();     
        }
    }
}

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