Contenido principal

Call .NET Methods with Parameter Modifiers

A .NET method can modify parameters using keywords such as ref, out, or params. MATLAB® maps these modifiers to method access attributes, allowing you to call such methods directly from MATLAB.

Call Method with ref Keyword

A parameter with the ref keyword is passed by reference, allowing the method to modify its value. This example shows how to pass an argument to a parameter defined with the ref keyword. To run this example, build and load the assembly created from this SampleRefTest C# Source Code using the directions in Build and Load .NET Assembly for MATLAB.

C# Method Signature

The db1 parameter in this method is modified by the ref keyword.

public void refTest(ref double db1)

Call Method from MATLAB

First, display the refTest method signature in MATLAB.

methodsview("netdoc.SampleRefTest")
Return TypeNameArguments
double scalar db1refTest(netdoc this,
double scalar db1)

Then call the refTest method from within MATLAB.

mlClass = netdoc.SampleRefTest;
db2 = refTest(mlClass,6)
db2 =
    12

SampleRefTest C# Source Code

using System;
namespace netdoc
{
    public class SampleRefTest
    {
        //test ref keyword
        public void refTest(ref double db1)
        {
            db1 = db1 * 2;
        }
    }
}

Call Method with out Keyword

A parameter with the out keyword is intended to be set by the method and returned to the caller. This example shows how to pass an argument to a parameter defined with the out keyword. To run this example, build and load the assembly created from this SampleOutTest C# Source Code using the directions in Build and Load .NET Assembly for MATLAB.

C# Method

The db2 parameter in this method is modified by the out keyword.

public void outTest(double db1, out double db2)

Call Method from MATLAB

First, display the outTest method signature.

methodsview("netdoc.SampleOutTest")
Return TypeNameArguments
double scalar db2outTest(netdoc.SampleOutTest this,
double scalar db1)

Then call the outTest method from within MATLAB.

mlClass = netdoc.SampleOutTest;
db3 = outTest(mlClass,6)
db3 =
   14.1000

SampleOutTest C# Source Code

using System;
namespace netdoc
{
    public class SampleOutTest
    {
        //test out keyword
        public void outTest(double db1, out double db2)
        {
            db1 = db1 * 2.35;
            db2 = db1;
        }
    }
}

Call Method with params Keyword

A parameter with the params keyword allows the method to accept a variable number of arguments (as an array). This example shows how to pass an argument to a parameter defined with the params keyword. To run this example, build and load the assembly created from this SampleParamsTest C# Source Code using the directions in Build and Load .NET Assembly for MATLAB.

C# Method

The num parameter in this method is modified by the params keyword.

public int paramsTest(params int[] num)

Call Method from MATLAB

First, display the paramsTest method signature.

methodsview("netdoc.SampleParamsTest")
Return TypeNameArguments
int32 scalar RetValparamsTest(netdoc.SampleParamsTest this,
System.Int32[] num)

Then call the paramsTest method from within MATLAB.

mlClass = netdoc.SampleParamsTest;
mat = [1, 2, 3, 4, 5, 6];
dbsum = paramsTest(mlClass,mat)
dbsum =
          21

SampleParamsTest C# Source Code

using System;
namespace netdoc
{
    public class SampleParamsTest
    {
        //test params keyword
        public int paramsTest(params int[] num)
        {
            int total = 0;
            foreach (int i in num)
            {
                total = total + i;
            }
            return total;
        }
    }
}

See Also

Functions

Topics