Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MATLAB/EXCEL through COM

149 views
Skip to first unread message

John

unread,
Nov 11, 2007, 8:25:40 PM11/11/07
to
Dear all,

I am trying to call MATLAB program from EXCEL through COM,
without using EXCEL/Link or any other MATLAB package. I am
having trouble to use handle.feval function from within
EXCEL. MATLAB did a poor job here-the document does not
mention this anywhere: in the following code,

ewmaRet = MatLab.Feval("strcat", 1, "hello", " world")

does not run. This statement directly comes from MATLAB
"External Interface Guide" doc.

I tried to look for the definition of feval (COM) from EXCEL :

Sub Feval(bstrName As String, nargout As Long, pvarArgOut,
[arg1], [arg2], [arg3], [arg4], [arg5], [arg6], [arg7],
[arg8], [arg9], [arg10], [arg11], [arg12], [arg13], [arg14],
[arg15], [arg16], [arg17], [arg18], [arg19], [arg20],
[arg21], [arg22], [arg23], [arg24], [arg25], [arg26],
[arg27], [arg28], [arg29], [arg30], [arg31], [arg32])
Member of MLApp.IMLApp
method Feval

What does pvarArgOut stand for?

Thank you for your help.

John

Dim MatLab As Object ' Create Object variable
Dim Result As String ' Create String variable
Dim ccRet() As Variant ' Create SAFEARRAY type
for Get/PutFullMatrix method
Dim ewmaRet As Object ' Create Object as an return
variable from MATLAB server

' Either way below will work.
Set MatLab = CreateObject("Matlab.Application")
MatLab.Execute ("enableservice('automationserver',true)")

MatLab.Visible = 1 ' Hide MATLAB application

'Calling m-file from VB
Result = MatLab.Execute("cd C:\Users")
Result = MatLab.Execute("addpath C:\Users")

' How to pass the value of a range of EXCEL Cells to Matlab
as an double array
' You have to do it in two steps:
' 1.) In EXCEL VBA, pass the value of the cells to a variant
in VBA
' 2.) Pass the variant to MATLAB by using PutWorkspaceData.
The passed variable
' becomes an cell array in MATLAB (server side).
' 3.) Ran MATLAB command to convert cell array to matrix of
double
ccRet = Sheet2.Range("C5", Range("C5").End(xlDown)).Value
Call MatLab.PutWorkspaceData("ccRet", "base", ccRet)
Call MatLab.Execute("ccRet = cell2mat(ccRet)")

ewmaRet = MatLab.Feval("strcat", 1, "hello", " world")


'Result = MatLab.Execute("surf(peaks)")
'Result = MatLab.Execute("a = [1 2 3 4; 5 6 7 8]")
'Result = MatLab.Execute("b = a + a ")
'
'Bring matrix b into EXCEL program
'Call MatLab.GetFullMatrix("b", "base", MReal, MImag)
'Call MatLab.GetWorkspaceData("b", "base", Result2)
'Call MatLab.GetWorkspaceData("b", "base", Range("E1"))
'Range("A1:D2").Value = Result2

' Clean up workspace in Matlab automation server process
'MatLab.Execute ("clear all; clc; clear global all")

End Sub

Matt R

unread,
Dec 4, 2007, 3:29:52 PM12/4/07
to
John,

pvarArgOut is the return value for the function called
through Feval. You should be passing it a pointer to (or a
reference depending on your language) a VARIANT. You code
is trying to pass it a string values, that's why it won't run.

Try this:

**** VB Code

Dim returnValue
ewmaRet = MatLab.Feval("strcat", 1, returnValue, "hello", "
world")

**** End VB Code

After this statement executes, returnValue should be an
array containing a single string whose value is "helloworld"

Hope that helps,
Matt


"John " <johnzli...@mathworks.com> wrote in message
<fh8a2k$e0m$1...@fred.mathworks.com>...

0 new messages