Sending data to MatLab using PutWorkspaceData

10 次查看(过去 30 天)
I am having trouble getting the correct syntax for passing data into MatLab using Excel/VBA.
I have copied the example from the help document http://www.mathworks.com/help/techdoc/ref/putworkspacedata.html but receive the following error:
Dim Matlab As Object
Dim data(6) As Double
Dim i As Integer
MatLab = CreateObject("matlab.application")
For i = 0 To 6
data(i) = i * 15
Next i
MatLab.PutWorkspaceData("A","base",data)
MsgBox("In MATLAB, type" & vbCrLf & "A")
"Compile Error: Syntax Error" around the following line of code:
Matlab.PutWorkspaceData("A", "base", data)
What is the correct syntax for this call?
Thank You, Craig
PS. I'm using Excel 2007 with VB 6.5 and Matlab R2010a

采纳的回答

Craig
Craig 2011-12-20
Solution ID: 1-8M7ZNR found here <http://www.mathworks.com/support/solutions/en/data/1-8M7ZNR/index.html?product=EL&solution=1-8M7ZNR> provides a working example of sending and getting data to Matlab. I unfortunately cannot figure out how I ran across this help document but the solution works for my environment.
Two points to make:
1) Essentially the data in excel is an array of doubles but a variant is used to eventually send the data to MatLab. The MatLab help mentions that a variant should be used but skips this conversion step in example provided within.
2) The solution uses the "Call" command in excel to remove any syntax errors. I used "Result =" which seems to also be acceptable.
Cheers,
Craig

更多回答(1 个)

Sean de Wolski
Sean de Wolski 2011-12-20
Try single quotes:
Matlab.PutWorkspaceData('A', 'base', data)
MATLAB uses single quotes to denote strings.
  2 个评论
Craig
Craig 2011-12-20
The single quotes didn't help. It returned the same error. I was however able to resolve the syntax error by placing a String variable in front of the expression. The prototype definition found in the help file does not have this documented.
Result = Matlab.PutWorkspaceData("A", "base", A)
The compiler now states "Compiler Error: Type mismatch" and highlights _.PutWorkspaceData_ when the error occurs.
Dushyant Dhundara
Dushyant Dhundara 2016-5-29
Hi, I am trying to mimic your code but for C#. it almost works, but in matlab it window when I type A, it only outputs the very last value. Did that happen with you ?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write COM Applications to Work with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by