Thread Subject: MLGetVar to return Matlab matrix var to VBA array var

Subject: MLGetVar to return Matlab matrix var to VBA array var

From: ckosmas

Date: 9 Feb, 2012 19:12:18

Message: 1 of 2

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.

Subject: MLGetVar to return Matlab matrix var to VBA array var

From: ckosmas

Date: 19 Mar, 2012 18:40:14

Message: 2 of 2

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.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mlgetvar ckosmas 9 Feb, 2012 14:14:32
excel ckosmas 9 Feb, 2012 14:14:32
vba ckosmas 9 Feb, 2012 14:14:32
matlab ckosmas 9 Feb, 2012 14:14:32
array ckosmas 9 Feb, 2012 14:14:32
matrix ckosmas 9 Feb, 2012 14:14:32
rssFeed for this Thread

Contact us at files@mathworks.com