MATLAB Answers

How can I use in MATLAB a COM object that uses SafeArray(string) as arguments?

11 views (last 30 days)
I have created a C# COM object following the solution:
When I implement a method with a Return Type of SafeArray(int32) and takes arguments (handle, SafeArray(string)).
Here is an example method implementation that will cause this particular error message.
public int[] StringLengths (string[] aryStrings)
int[] result = new int[aryStrings.Length];
for ( int i = 0; i < aryStrings.Length; i++)
result[i] = aryStrings[i].Length;
return result;
This method returns the number of characters in the string argument.
I call it in MATLAB using:
h.StringLengths({'Hello jiaying'})
??? Error: The parameter is incorrect.
StringLengths has a Return Type of SafeArray(int32) and takes arguments (handle, SafeArray(string))
I receive the following error message:
??? Error: The parameter is incorrect.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This is the expected functionality. MATLAB defaults to using two-dimensional arrays; this function requires single-dimensional arrays.
To work around this issue, configure MATLAB to export single-dimensional arrays by executing this command:
feature('COM_SafeArraySingleDim', 1)
This forces MATLAB to export single dimension arrays instead of two-dimensional when exporting arrays to COM objects:
h.StringLengths({'Hello jiaying'})
ans =
Note that setting this feature may break COM interaction with Excel, including Excel Link. If you wish to reset the default behavior of MATLAB of passing all data as two-dimensional arrays, execute:
feature('COM_SafeArraySingleDim', 0)

More Answers (0)




Community Treasure Hunt

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

Start Hunting!