How do I get status information or error messages when using Excel Link functions in VBA macros?

2 views (last 30 days)
If there is an error when executing an Excel Link command in a VBA macro, there is no indication of an error. For example, using MLGetMatrix when the variable does not exist appears to succeed, but no data is copied. The same is true with MLEvalString failures.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Apr 2012
This change has been incorporated into the documentation in Release 2012a (R2012a). For previous releases, read below for any additional information:
When used in a VBA macro, all Excel Link functions return a single output which indicates the status of the operation.
For example, the MLGetMatrix function can be used as follows:
status = MLGetMatrix(var_name, edat)
The "status" output variable is set to the number "0" if the command succeeded. If the command fails an error code or error message is returned as a string. Please refer to the link at the bottom of the page for a table which describes the error codes.
For example, you could use the following function to get the variable "A" from MATLAB and test to see if the command succeeded. If it failed, a message box is displayed with the error message.
Sub myfun()
Dim result As Variant
result = MLGetMatrix("A", "A1")
If result <> 0 Then
MsgBox "There was an error getting the matrix A: " & result
End If
MatlabRequest
End Sub
As of Excel Link 2.4 (R2006b), you optionally have the actual MATLAB error message returned with Excel Link. This option is configured using the function MLShowMatlabErrors. For more information execute the following command at the MATLAB prompt:
doc MLShowMatlabErrors
This status output can optionally be returned from any of the following functions:
MLAppendMatrix
MLAutoStart
MLClose
MLDeleteMatrix
MLEvalString
MLGetFigure
MLGetMatrix
MLGetVar
MLOpen
MLPutMatrix
MLPutVar
MLShowMatlabErrors
MLStartDir
MLUseFullDesktop
For more information on a particular function use the DOC command followed by the command name at the MATLAB command prompt. For example:
doc MLAppendMatrix

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!