Main Content

Code Multiple Outputs for C# .NET Client

MATLAB® allows users to write functions with multiple outputs. To code multiple outputs in C#, use the out keyword.

The following MATLAB code takes multiple inputs (i1, i2, i3) and returns multiple outputs (o1, o2, o3), after performing some checks and calculations.

In this example, the first input and output are of type double, and the second input and output are of type int. The third input and output are of type char.

To deploy this function with MATLAB Production Server™ software, you need to write a corresponding method interface in C#, using the out keyword. Specifying the out keyword causes arguments to be passed by reference. When using out, ensure both the interface method definition and the calling method explicitly specify the out keyword.

The output argument data types listed in your C# interface (referenced with the out keyword) must match the output argument data types listed in your MATLAB signature exactly. Therefore, in the C# interface (MultipleOutputsExample) and method (TryMultipleOutputs) code samples, multiple outputs are listed (with matching specified data types) in the same order as they are listed in your MATLAB function.

 MATLAB Function multipleoutputs

 C# Interface MultipleOutputsExample

 C# Method TryMultipleOutputs

After creating a new instance of MWHttpClient and a client proxy, variables and the calling method, multipleoutputs, are declared.

In the multipleoutputs method, values matching each declared types are passed for output (1.2 for double, 10 for int, and hello for string) to output1.

Note the following coding best practices illustrated by this example:

  • Both the MATLAB function signature and the C# interface method signature use the name multipleOutputs. Both MATLAB and C# code are processing three inputs and three outputs.

  • MATLAB .NET interface supports direct conversion from C# double array to MATLAB double array and from C# string to MATLAB char array. For more information, see Data Conversion with C# and MATLAB Types and Conversion Between MATLAB Types and C# Types.