MATLAB: How to pass a structure from MATLAB into VBA using Excel Link

MATLAB Builder EX

I would like to pass MATLAB structures from MATLAB to Excel using VBA. However, the MLGetVar and MLGetMatrix functions do not allow me to pass structures from MATLAB to VBA.

Best Answer

The MATLAB strucuture cannot be directly pulled into Excel. Instead, the elements of a structure need to be transferred manually. Here is a sample VBA code that creates a structure in MATLAB and pulls it into VBA:
Private Type myVBAStruct
Name As Variant
Height As Variant
End Type
Sub WriteFileName()
Dim Contact As myVBAStruct
MLEvalstring ("mystruct.name='somename'; mystruct.height=5.8;")
MLEvalstring ("temp = mystruct.name")
MLGetVar "temp", Contact.Name
Contact.Name = Contact.Name(1, 1)
MLEvalstring ("temp = mystruct.height")
MLGetVar "temp", Contact.Height
Contact.Height = Contact.Height(1, 1)
MsgBox (Contact.Height)
MsgBox (Contact.Name)
End Sub