Why do I get the error "Too many output arguments." when passing MWCharArray with MATLAB Builder for .NET 2.0 (R2006a)?

2 views (last 30 days)
I am calling a .NET object that I created with MATLAB Builder for .NET 2.0 (R2006a) from C#. I want to pass a string to one of the class methods using an MWCharArray. The class is called CharArrayCompclass and the method is DispCharArray. I am doing this as follows
CharArrayCompclass SpObj = new CharArrayCompclass();
MWCharArray x = new MWCharArray("hello");
SpObj.DispCharArray(x);
However, I am getting the following error message:
??? Error using ==> DispCharArray
Too many output arguments.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The documentation has been fixed in Release 2007b (R2007b).
For previous product releases, documentation on how to pass an MWCharArray as the first input parameter when using the single output signature is missing from MATLAB Builder for .NET 2.0 (R2006a).
Here is additional information.
When a function "foo" is compiled into a .NET class, several overloaded methods are created with variying signatures. One set of these signatures is called the single output signature which assumes that the M-function will return only one output. Another set of these signatures is the standard API where the number of output arguments is an integer passed into the function as the first input argument.
When using the single output signature format, you must pass in an MWArray or some other data type which .NET will not attempt to cast to an integer. In the case of an MWCharArray, the automatic cast is to a String which .NET then automatically casts to an integer thereby calling the standard API signature if this is the first input to a method of the .NET class.
To work around this issue, explicitly cast the MWCharArray to an MWArray:
MWCharArray x = new MWCharArray("hello");
CharArrayCompclass SpObj = new CharArrayCompclass();
SpObj.DispCharArray((MWArray)x);
or just pass in a regular string:
String s = "hello";
CharArrayCompclass SpObj = new CharArrayCompclass();
SpObj.DispCharArray(s);
The problem is due to an incorrect function signature being called. When an M-function is compiled into a .NET object, several function signatures are created. For example, the following M-function
function DispCharArray(mychararray)
has the following prototypes in C#:
DispCharArray(int, MathWorks.MATLAB.NET.Arrays.MWArray)
DispCharArray(int)
DispCharArray(MathWorks.MATLAB.NET.Arrays.MWArray)
DispCharArray()
C# is auto-casting the MWCharArray to an integer and calling the function with the prototype "DispCharArray(int)". This integer argument is used by the MCR to determine the number of output arguments to return, resulting in an error.

More Answers (0)

Products


Release

R2006a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!