Forcing a singleton to be treated as a SAFEARRAY

3 views (last 30 days)
I have a C# method that I want to call from MATLAB that takes as input an array of 'int32's (int[] in C#). The signature in MATLAB is like this:
MyMethod = handle MyMethod(handle, SafeArray(int32))
Sometimes I want the input array to only have one element. This is causing me problems since, whereas I can pass a matrix with multiple elements as a SAFEARRAY, if I have a 1x1 matrix it gives me an error, presumably because it's being passed as type int32 and not SafeArray(int32).
Is there any way I can create in MATLAB a SAFEARRAY with only one element? I can work around this issue by padding a 1x1 matrix with a dummy element, but I'd prefer to do it the right way.

Answers (1)

Martijn
Martijn on 25 Feb 2011
The default data conversion rules for MATLAB's COM interface convert all non-scalar MATLAB arrays into 2-dimensional SAFEARRAYs.
Beginning with MATLAB 7.0.4 (R14SP2) it is possible to work around the default data conversion rule and have MATLAB convert a MATLAB array into a single dimensional SAFEARRAY. You can pass data to ActiveX servers as one-dimensional arrays by executing the following command at the MATLAB prompt:
feature('COM_SafeArraySingleDim', 1)
After executing this command, MATLAB will convert any single column matrix to a 1 dimensional array when passed to a COM object.
On a whole different note though, since MATLAB 7.9 (R2009b) you can directly call .NET Assemblies so you do not have to call them through a COM Interface anymore:
  1 Comment
James King
James King on 25 Feb 2011
The problem is that I want to pass a singleton as a SafeArray. I have no problems passing a column vector as a one-dimensional SafeArray.
That other link is very helpful though; thanks!

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!