Discussion:
MLGetVar to return Matlab matrix var to VBA array var
(too old to reply)
ckosmas
2012-02-09 19:12:18 UTC
Permalink
Hi everyone,
my problem is that I am trying to take some data from VBA(Excel) to Matlab, process them, and return the results to VBA. While I can send both array and single number variables from VBA to Matlab quite successfully using MLPutVar, I can only return single number variables from Matlab to VBA using MLGetVar. Whenever I try to return a matrix and have VBA write it to an array variable, I end up with either an empty array in VBA (whereas the corresponding Matlab matrix is perfectly full of data), or with jut a single number variable, containing only the first element of the corresponding Matlab array.

To make things more clear, my Matlab code (for the purpose of this problem only) is:
c = linspace(24,1,24)
a = 1

and my VBA code is:
Sub ....()
Dim MatLab As Object
Dim ccc(1 To 24) As Variant

Set MatLab = CreateObject("Matlab.desktop.Application")
matlabinit
Call MatLab.Execute("cd C:\....\....")
Call MatLab.Execute("matlab_excel")

'this returns a single number var, and works fine
mlgetvar "a", aaa
Range("A1") = aaa

'this returns matrix "c" to VBA by writing it on variable "ddd" and works partially, as it only returns element c(1)
mlgetvar "c", ddd
Range("A2") = ddd

'this fails to return matrix "c" to VBA by writing it on array "ccc", ccc results as empty array
mlgetvar "c", ccc
Range("A2") = ccc(24)

'this fails to return matrix "c" directly to Excel, but cells remain empty
mlgetmatrix "c", Range("A3")

End Sub

* both xlsm and m files are in the same folder (otherwise nothing worked... anyone knows why?)
**all necessary VBA references have been added (Matlab automation server type library, SpreadsheetLink2007_2010)
***the Spreadsheet Link Ex add-in has been added in Excel
****If I dimension the VBA array as anything other than variant (e.g. double, which would make sense as Matlab's matrix is also type: double) I get a run-time error: "Variable uses Automation type not supported in VBA".

Any ideas? Is a Matlab matrix somehow incompatible with a VBA array? Why can I only return single number variables to VBA? Many thanks in advance.
ckosmas
2012-03-19 18:40:14 UTC
Permalink
Installing Office 2010 x64 may be expected to help (as my Matlab 2010b is also x64), however still I don't get a seamless cooperation between excel, vba and Matlab. MLGetVar and MLPutVar still don't work, though I 've managed to cover some ground using Matlab BuilderEX - but still even that doesn't work seamlessly for me (I've been unable to call the compiled functions through VBA when they return array results, whereas on worksheet level this more or less works). However, what little level of cooperation I 've so far been able to achieve has only been after installing Excel 2010 x64.
Loading...