Pass System.Nullable Arguments
The example uses the NetDocNullable assembly to show how to call a
method with a System.Nullable input argument. It uses the MATLAB®
plot function to show how to handle a
System.Nullable output argument.
Suppose that you have this C# signature for the SetField method
that has System.Nullable parameters.
public Nullable<double> SetField(Nullable<double> db)The function signature in MATLAB is:
| Return Type | Name | Arguments |
|---|---|---|
System.Nullable | SetField | (NetDocNullable. |
For an example, build the NetDocNullable assembly from this C#
source code using the directions in Build and Load .NET Assembly for MATLAB. NetDocNullable defines method SetField which
has System.Nullable arguments.
using System;
namespace NetDocNullable
{
public class MyClass
{
private Nullable<double> myField = null;
public Nullable<double> GetField()
{
return myField;
}
public Nullable<double> SetField(Nullable<double> db)
{
myField = db;
return myField;
}
}
}Create a mlClass variable to call SetField.
MATLAB automatically converts double and
null values to
System.Nullable<System*Double> objects.
mlClass = NetDocNullable.MyClass;
Call SetField with a double value.
field1 = SetField(mlClass,10)
field1 =
System.Nullable<System*Double>
Package: System
Properties:
HasValue: 1
Value: 10
Methods, Superclasses
The HasValue property is true (1) and the
Value property is 10.
Pass null value [].
field2 = SetField(mlClass,[])
field2 =
System.Nullable<System*Double>
Package: System
Properties:
HasValue: 0
Methods, Superclasses
The HasValue property is false (0), and it has
no Value property.
Pass System.Nullable Argument to SetField Method
Suppose that you have this C# signature for the SetField method
that has System.Nullable parameters.
public Nullable<double> SetField(Nullable<double> db)The function signature in MATLAB is:
| Return Type | Name | Arguments |
|---|---|---|
System.Nullable | SetField | (NetDocNullable. |
To run the example, build the NetDocNullable assembly from this
C# source code using the directions in Build and Load .NET Assembly for MATLAB. NetDocNullable defines method SetField
which has System.Nullable arguments.
using System;
namespace NetDocNullable
{
public class MyClass
{
private Nullable<double> myField = null;
public Nullable<double> GetField()
{
return myField;
}
public Nullable<double> SetField(Nullable<double> db)
{
myField = db;
return myField;
}
}
}Create a mlClass variable to call SetField.
MATLAB automatically converts double and
null values to
System.Nullable<System*Double> objects.
mlClass = NetDocNullable.MyClass;
Call SetField with a double value.
field1 = SetField(mlClass,10)
field1 =
System.Nullable<System*Double>
Package: System
Properties:
HasValue: 1
Value: 10
Methods, Superclasses
The HasValue property is true (1) and the
Value property is 10.
Pass null value [].
field2 = SetField(mlClass,[])
field2 =
System.Nullable<System*Double>
Package: System
Properties:
HasValue: 0
Methods, Superclasses
The HasValue property is false (0), and it
has no Value property.
Handle System.Nullable Output Arguments in MATLAB
Before you use a System.Nullable object in MATLAB, first decide how to handle null values. If you
ignore null values, you might get unexpected results when you use
the value in a MATLAB function.
The System.Nullable class provides two techniques for handling
null values. To provide special handling for null values, use the
HasValue property. To treat a null value
in the same way as a double, use the
GetValueOrDefault method.
Create a MATLAB function, plotValue.m, which detects
null values and treats them differently from numeric values.
The input is a System.Nullable<System*Double> type. If the
input is null, the function displays a message. If the input
value is double, it creates a line graph from
0 to the value.
function plotValue(x) % x is System.Nullable<System*Double> type if (x.HasValue && isfloat(x.Value)) plot([0 x.Value]) else disp("No Data") end
The plotValue function uses the HasValue
property of the input argument to detect null values and calls
the MATLAB
plot function using the Value
property.
Call plotValue with variable field1 to
display a line graph.
plotValue(field1)
Call plotValue with the variable field2, a
null value.
plotValue(field2)
No Data
If you do not need special processing for null values, use the
GetValueOrDefault method. To display the
GetValueOrDefault function signature, type:
methodsview(field1)
Look for the GetValueOrDefault function signature:
| Name | Return Type | Arguments |
|---|---|---|
GetValueOrDefault | double scalar RetVal | (System.Nullable |
This method converts the input variable to double so you can
directly call the MATLAB
plot function:
myData = GetValueOrDefault(field1); plot([0 myData+2])
The GetValueOrDefault method converts a null
value to the default numeric value, 0.
defaultData = GetValueOrDefault(field2)
defaultData =
0Call plot:
plot([0 defaultData])
You can change the default value using the GetValueOrDefault
method. Open the methodsview window and look for the
GetValueOrDefault function signature:
| Name | Return Type | Arguments |
|---|---|---|
GetValueOrDefault | double scalar RetVal | (System.Nullable |
Set the defaultValue input argument to a new value,
-1, and plot the results for null value
field2.
defaultData = GetValueOrDefault(field2,-1); plot([0 defaultData])