The Excel Link 2.4 (R2006b) documentation states that MLGetVar can be used in macro subroutines but not in functions. However, the following VBA custom function seems to work:
Function foo() Dim returnValue As Variant On Error GoTo Handle MLEvalString "s = 99;" MLGetVar "s", returnValue foo = returnValue Exit FunctionHandle: StringTest = "Error: " + Err.DescriptionEnd Function
When I use the following Excel Link based function:
Function badGetVarFunc(varX As Variant) As Variant On Error GoTo ErrorHandler MLShowMatlabErrors "yes" MLEvalString "clear" MLPutVar "x0", varX MLEvalString "y0=-2*x0" MLGetVar "y0", varY badGetVarFunc = varY Exit Function ErrorHandler: MsgBox (Err.Description) End Function
in an Excel cell:
=badGetVarFunc(13)
Best Answer