| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB Builder EX |
| Contents | Index |
| Learn more about MATLAB Builder EX |
| On this page… |
|---|
Adding the MATLAB Builder EX COM Function to Microsoft Excel |
The M-file mymagic takes a single input, an integer, and creates a magic square of that size.
The Microsoft Excel file mymagic.xls uses this function in three different ways:
Output Magic Square Results to Microsoft Excel calls the function mymagic with a value of 4. The function returns a magic square of size 4 and populates a range of Excel cells with that magic square.
Transpose the Output uses the transpose flag to transpose a magic square of size 4.
Resize the Output resizes the output to a higher value and moves its location within the Excel worksheet.
From the MATLAB command prompt, change folders to myfiles\work.
If you have not already done so, execute the following command in the MATLAB prompt:
mbuild -setup
Be sure to choose a supported compiler. See Supported Compilers.
Enter the deploytool command to open the Deployment Tool.
Use the following information as you work through this example using the instructions in Building Your Component:
| Project Name | xlmagic |
| Class Name | xlmagicclass |
| File to compile | mymagic.m |
Start Microsoft Excel on your system.
Open the file myfiles\work\xlmagic\mymagic.xls.
From the Excel main window (not the Microsoft Visual Basic Editor), open the Macro dialog box by pressing the Alt and F8 keys simultaneously, or by selecting Tools > Macro > Macros.
Select mymagic from the list and click Run. This procedure returns a magic square of size 4 beginning in cell B2.

Reopen the Macro dialog box, select the mymagic_transpose macro and click Run. This procedure returns a magic square of size 4 transposed, beginning in cell B14.

Reopen the Macro dialog box, select the mymagic_resize macro, and click Run. This procedure returns a magic square of size 4 beginning in cell B32.
Change the value of 4 in cell A32 to a higher value and rerun this macro. A magic square of the size you specified in cell A32 is returned, beginning in cell B32.

From the Excel main window, select Tools > Macro > Visual Basic Editor.
When the Visual Basic Editor opens, in the Project - VBAProject window, double-click to expand VBAProject (mymagic.xls).
Expand the Modules folder and double-click the Module1 module.
This opens the VB Code window with the code for this project.
![]() | Usage Examples | Multiple Files and Variable Arguments Example | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |