Documentation

Object Passing by Reference

MATLAB Array

MWObjectArray, a special subclass of MWArray, lets you create a MATLAB® array that references .NET objects.

Note

For information about these data conversion classes, see the MATLAB MWArray Class Library Reference, available in the matlabroot\help\dotnetbuilder\MWArrayAPI folder, where matlabroot represents your MATLAB installation folder

Wrapping and Passing .NET Objects with MWObjectArray

You can create a MATLAB code wrapper around .NET objects using MWObjectArray. Use this technique to pass objects by reference to MATLAB functions and return .NET objects. The examples in this section present some common use cases.

Passing a .NET Object into a MATLAB Compiler SDK .NET Assembly

To pass an object into a MATLAB Compiler SDK™ assembly:

  1. Write the MATLAB function that references a .NET type:

    function addItem(hDictionary, key, value)
    
           if ~isa(hDictionary,'System.Collections.Generic.IDictionary')
               error('foo:IncorrectType', 
                 ... 'expecting a System.Collections.Generic.Dictionary');
           end 
         
           hDictionary.Add(key, value);
     
         end
    

  2. Create a .NET object to pass to the MATLAB function:

         Dictionary char2Ascii= new Dictionary();
         char2Ascii.Add("A", 65);
         char2Ascii.Add("B", 66);
    
  3. Create an instance of MWObjectArray to wrap the .NET object:

     MWObjectArray MWchar2Ascii= 
               new MWObjectArray(char2Ascii);
    
  4. Pass the wrapped object to the MATLAB function:

    myComp.addItem(MWchar2Ascii,'C', 67);
    

Returning a Custom .NET Object in a MATLAB Function Using a Deployed MATLAB Compiler SDK .NET Assembly

You can also use MWObjectArray to clone an object inside a MATLAB Compiler SDK .NET Assembly. Continuing with the example in Passing a .NET Object into a MATLAB Compiler SDK .NET Assembly, perform the following steps:

  1. Write the MATLAB function that references a .NET type:

     function result= add(hMyDouble, value)
    
           if ~isa(hMyDouble,'MyDoubleComp.MyDouble')
              error('foo:IncorrectType', 'expecting a MyDoubleComp.MyDouble');
           end
           hMyDoubleClone= hMyDouble.Clone();
           result= hMyDoubleClone.Add(value);
     
         end
    
  2. Create the object:

    MyDouble myDouble= new MyDouble(75);
    
  3. Create an instance of MWObjectArray to wrap the .NET object:

    MWObjectArray MWdouble= new MWObjectArray(myDouble);
                origRef = new MWObjectArray(hash);      

  4. Pass the wrapped object to the MATLAB function and retrieve the returned cloned object:

    MWObjectArray result= 
                (MWObjectArray)myComp.add(MWdouble, 25);
  5. Unwrap the .NET object and print the result:

    MyDouble doubleClone= (MyDouble)result.Object;
    
         Console.WriteLine(myDouble.ToDouble());
         Console.WriteLine(doubleClone.ToDouble());
    

Cloning an MWObjectArray

When calling the Clone method on MWObjectArray, the following rules apply for the wrapped object.

  • If the wrapped object is a ValueType, it is deep-copied.

  • If an object is not a ValueType and implements ICloneable, the Clone method for the object is called.

  • The MemberwiseClone method is called on the wrapped object.

 Calling Clone on MWObjectArray

Optimization Example Using MWObjectArray

For a full example of how to use MWObjectArray to create a reference to a .NET object and pass it to a component, see Integrating MATLAB Optimization Routines with Objective Functions in .NET and Integrating MATLAB Optimization Routines with Objective Functions in Visual Basic.

MWObjectArray and Application Domains

Every ASP .NET web application deployed to IIS is started in a separate AppDomain.

The MATLAB .NET interface must support the .NET type wrapped by MWObjectArray. If the MWObjectArray is created in the default AppDomain, the wrapped type has no other restrictions.

If the MWObjectArray is not created in the default AppDomain, the wrapped .NET type must be serializable. This limitation is imposed by the fact that the object needs to be marshaled from the non-default AppDomain to the default AppDomain in order for MATLAB to access it.

MWObjectArray Limitation

If you have any global objects in your C# code then you will get a Windows® exception on exiting the application. To overcome this limitation, use one of these solutions:

  • Explicitly clear global objects before exiting the application.

    globalObj.Destroy();

  • Call TerminateApplicationEx method before exiting the application.

    MWMCR.TerminateApplicationEx();

    For more information on TerminateApplicationEx, see MWArray API Documentation.