How do I return multiple arguments from a MATLAB Excel Builder add-in function?

2 views (last 30 days)
I would like to make an Excel Builder function return two or more output parameters without requiring a separate sub-routine to populate a cell range.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
To return two or more output parameters, slightly modify the VBA code that is automatically generated by Excel Builder.
For example, if you compile a function FOO into an Excel add-in function, modify the generated VBA code as follows:
Function foo(x1 As Variant, x2 As Variant) As Variant
Dim aClass As Object
Dim y1, y2 As Variant
On Error Goto Handle_Error
aClass = CreateObject("foocomponent.fooclass.1_0")
'Specify the number of outputs as 2, and specify the two output variables
Call aClass.foo(2,y1,y2,x1,x2)
'Create an array from the outputs using the Array function
allouts = Array(y1,y2)
'Return the array
foo = allouts
Exit Function
Handle_Error:
foo = Err.Description
End Function
When executing this function in an Excel worksheet, execute the function as an array function (also known as array formula or CSE formula). This means that you need to make a (horizontal, vertical or area) selection of cells corresponding to the number of output arguments of the function, then you need to press F2 and enter the formula normally. When you have done so, press Ctl+Shift+Enter to confirm the edit. As a result of this, the formula will be displayed by Excel in curly braces {}.

More Answers (0)

Categories

Find more on Data Export to MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!