Code Variable-Length Inputs and Outputs for .NET Client
MATLAB®
Production Server™ .NET client supports the MATLAB capability of working with variable-length inputs. See the
MATLAB Function Reference for complete information on
varargin
and varargout
.
Using varargin with .NET Client
You pass MATLAB variable input arguments (varargin
) using the C#
params
keyword.
For example, consider the MATLAB function varargintest
, which takes a
variable-length input (varargin
)—containing strings and
integers—and returns an array of cell
s
(o
).
function o = varargintest(s1, i2, varargin) o{1} = s1; o{2} = i2; idx = 3; for i=1:length(varargin) o{idx} = varargin{i}; idx = idx+1; end
The C# interface VararginTest
implements the MATLAB function varargintest
.
public interface VararginTest { object[] varargintest(string s, int i, params object[] objArg); }
Since you are sending output to cell
arrays in MATLAB, you define a compatible C# array type of object[]
in your interface. objArg
defines number of inputs
passed—in this case, two.
The C# method TryVarargin
implements
VararginTest
, sending two strings and two integers to the
deployed MATLAB function, to be returned as a cell
array.
public static void TryVarargin() { MWClient client = new MWHttpClient(); VararginTest mpsexample = client.CreateProxy<VararginTest>(new Uri("http://localhost:9910/mpsexample")); object[] vOut = mpsexample.varargintest("test", 20, false, new int[]{1,2,3}); Console.ReadLine(); }
Note the following coding best practices illustrated by this example:
Both the MATLAB function signature and the C# interface method signature use the name
varargintest
. Both MATLAB and C# code are processing two variable-length inputs, string and integer.MATLAB .NET interface supports direct conversion between MATLAB cell arrays and C# object arrays. See Data Conversion with C# and MATLAB Types and Conversion Between MATLAB Types and C# Types for more information.
Using varargout with .NET Client
MATLAB variable output arguments (varargout
) are obtained
by passing an instance of System.Object[]
array. The array is
passed with the attribute [varargout]
, defined in the
Mathworks.MATLAB.ProductionServer.Client.dll
assembly.
Before passing the System.Object[]
instance, initialize the
System.Object
array instance with the maximum length of the
variable in your calling method. The array is limited to one dimension.
For example, consider the MATLAB function varargouttest
, which takes one
variable-length input (varargin
), and returns one variable-length
output (varargout
), as well as two non-variable-length outputs
(out1
and out2
).
functionout [out1 out2 varargout] = varargouttest(in1, in2, varargin) out1 = modifyinput(in1); out2 =modifyinput(in2); for i=1:length(varargin) varargout{i} = modifyinput(varargin{i}); end function out = modifyinput(in) if ( isnumeric(in) ) out = in*2; elseif ( ischar(in) ) out = upper(in); elseif ( islogical(in) ) out = ~in; else out = in; end
Implement MATLAB function varargouttest
with the C# interface
VarargoutTest
.
In the interface method varargouttest
, you define multiple
non-variable-length outputs (o1
and o2
, using
the out
keyword, described in Code Multiple Outputs for C# .NET Client), a
double
input (in1
) and a
char
input (in2
).
You pass the variable-length output (o3
) using a
single-dimensional array (object[]
with attribute
[varargout]
), an instance of
System.Object[]
.
As with Using varargin with .NET Client, you use the C#
params
keyword to pass the variable-length input.
public interface VarargOutTest { void varargouttest(out double o1, out string o2, double in1, string in2, [varargout]object[] o3, params object[] varargIn); }
In the calling method TryVarargout
, note that both the type and
length of the variable output (varargOut
) are being passed
((short)12
).
public static void TryVarargout() { MWClient client = new MWHttpClient(); VarargOutTest mpsexample = client.CreateProxy<VarargOutTest>(new Uri("http://localhost:9910/mpsexample")); object[] varargOut = new object[3]; // get all 3 outputs double o1; string o2; mpsexample.varargouttest(out o1, out o2, 1.2, "hello", varargOut, true, (short)12, "test"); varargOut = new object[2]; // only get 2 outputs double o11; string o22; mpsexample.varargouttest(out o11, out o22, 1.2, "hello", varargOut, true, (short)12, "test"); }
Note
Ensure that you initialize varargOut
to the appropriate
length before passing it as input to the method
varargouttest
.
Note the following coding best practices illustrated by this example:
Both the MATLAB function signature and the C# interface method signature use the name
varargouttest
. Both MATLAB and C# code are processing a variable-length input, a variable-length output, and two multiple non-variable-length outputs.MATLAB .NET interface supports direct conversion between MATLAB cell arrays and C# object arrays. See Data Conversion with C# and MATLAB Types and Conversion Between MATLAB Types and C# Types for more information.