Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Builder EX   

Magic Square Example

Overview

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:

Creating the Project

  1. From the MATLAB command prompt, change folders to myfiles\work.

  2. 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.

  3. Enter the deploytool command to open the Deployment Tool.

  4. Use the following information as you work through this example using the instructions in Building Your Component:

    Project Namexlmagic
    Class Namexlmagicclass
    File to compilemymagic.m

Adding the MATLAB Builder EX COM Function to Microsoft Excel

  1. Start Microsoft Excel on your system.

  2. Open the file myfiles\work\xlmagic\mymagic.xls.

      Note   If an Excel prompt says that this file contains macros, click Enable Macros to run this example.

Output Magic Square Results to Microsoft Excel

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.

Transpose the Output

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.

Resize the Output

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.

Inspecting the Microsoft Visual Basic Code

  1. From the Excel main window, select Tools > Macro > Visual Basic Editor.

  2. When the Visual Basic Editor opens, in the Project - VBAProject window, double-click to expand VBAProject (mymagic.xls).

  3. Expand the Modules folder and double-click the Module1 module.

    This opens the VB Code window with the code for this project.

  


Recommended Products

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