Main Content

Pass System.Nullable Arguments

This example shows how to handle .NET methods with System.Nullable type arguments, whose underlying value type is double.

The example shows how to call a method with a System.Nullable input argument. It uses the MATLAB® plot function to show to handle a System.Nullable output argument.

Build Custom Assembly NetDocNullable

To execute the MATLAB code in this example, build the NetDocNullable assembly. The assembly is created with the C# code, NetDocNullable.cs, in the matlabroot/extern/examples/NET/NetSample folder. To see the code, open the file in MATLAB Editor and build the NetDocNullable assembly.

NetDocNullable defines method SetField which has System.Nullable arguments.

 SetField Function Signature

Load NetDocNullable Assembly

The example assumes that you put the assembly in your c:\work folder. You can modify the example to change the path, dllPath, of the assembly.

dllPath = fullfile('c:','work','NetDocNullable.dll');
asm = NET.addAssembly(dllPath);
cls = NetDocNullable.MyClass;

Use the cls variable to call SetField, which creates a System.Nullable<System*Double> value from your input.

Pass System.Nullable Input Arguments

MATLAB automatically converts double and null values to System.Nullable<System*Double> objects.

Pass a double value.

field1 = SetField(cls,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(cls,[])
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 following function signature:

 GetValueOrDefault Function Signature

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 =
     0

Call plot:

plot([0 defaultData])

You can change the default value using the GetValueOrDefault method. Open the methodsview window and look for the following function signature:

 GetValueOrDefault Function Signature to Change Default

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])

Related Examples

More About