필터 지우기
필터 지우기

Forcing a singleton to be treated as a SAFEARRAY

조회 수: 2 (최근 30일)
James King
James King 2011년 2월 25일
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.

답변 (1개)

Martijn
Martijn 2011년 2월 25일
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
James King
James King 2011년 2월 25일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by