MATLAB Answers

0

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

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=actxserver('Test.Libray')
h.StringLengths({'Hello jiaying'})
??? Error: The parameter is incorrect.
From
methodsview(h)
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.

Products


Release

R2006b

1 Answer

Answer by MathWorks Support Team on 27 Jun 2009
 Accepted Answer

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 =
13
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)

  0 Comments

Sign in to comment.