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 Type | Name | Arguments |
|---|---|---|
double scalar db1 | refTest | (netdoc this, |
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 Type | Name | Arguments |
|---|---|---|
double scalar db2 | outTest | (netdoc.SampleOutTest this, |
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 Type | Name | Arguments |
|---|---|---|
int32 scalar RetVal | paramsTest | (netdoc.SampleParamsTest
this, |
Then call the paramsTest method from within MATLAB.
mlClass = netdoc.SampleParamsTest; mat = [1, 2, 3, 4, 5, 6]; dbsum = paramsTest(mlClass,mat)
dbsum =
21SampleParamsTest 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;
}
}
}