MATLAB: How to pass arguments to an ActiveX server from MATLAB 7.0 (R14) as one-dimensional arrays

actxcontrolarraybasicdimensiondimensionalitydimensionsMATLABsafearrayvbvisual

I have created an ActiveX server that I instantiate in MATLAB with ACTXSERVER. I would like to pass data from MATLAB to a server method as a one-dimensional array, but my arguments are converted to two-dimensional arrays when passed.
One example of this issue is when using the ADODB.Stream ActiveX Server. When executing the following code
a=uint8(zeros(23,1));
sqlstream=actxserver('ADODB.Stream')
sqlstream.Type = 1;
sqlstream.Open()
sqlstream.Write(a)
you receive the following error message
??? Invoke Error, Dispatch Exception:
Source: ADODB.Stream
Description: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Help File: C:\WINNT\HELP\ADO270.CHM
Help Context ID: 12ee41
For different COM objects, the error message returned will be different.

Best Answer

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.
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)
You can also query the current state of this property with the command
feature('COM_SafeArraySingleDim')
In the case of the example with the ADODB.Stream object given above, you could use the following code to work around the issue. In this example, the COM_SafeArraySingleDim is only enabled for the single function call where it is required.
a=uint8(zeros(23,1));
sqlstream=actxserver('ADODB.Stream')
sqlstream.Type = 1;
sqlstream.Open()
feature('COM_SafeArraySingleDim', 1)
sqlstream.Write(a)
feature('COM_SafeArraySingleDim', 0)
More information about the default behavior of the MATLAB COM Interface data conversion is available here:
MATLAB COM Client Support :: COM and DDE Support (Windows Only) (External Interfaces)
If you are using MATLAB 7.0 (R14) or MATLAB 7.0.1 (R14SP1), you can download a revised version of comcli.dll that allows you to use the work around described above. For the required steps, see the Related Solution "Why am I having problems using the MATLAB 7.0 (R14) COM interface?"