Main Content

Create Macros with Graphical Functions

The Create Macro that Displays Graphical Output example shows you how to create an add-in and macro using a function that displays graphical output. For an example that uses a MATLAB function that displays a dialog box, see Create Macro That Displays Error Message.

Create Macro that Displays Graphical Output

Create an add-in and macro using a function that displays graphical output.

Create Add-In Using Function with Graphical Output

Build your COM component and add-in with MATLAB® Compiler™.

  1. Create a MATLAB function with a graphical output. For this example, create a function named mysurf.m.

    function mysurf
    surf(peaks);

  2. Build the Excel® add-in using the mysurf function with the Library Compiler app or compiler.build.excelAddIn.

    For example, if you are using compiler.build.excelAddIn, type:

    buildResults = compiler.build.excelAddIn('mysurf.m', ...
    'GenerateVisualBasicFile','on');

    For more details, see the instructions in Create Excel Add-In from MATLAB.

Create Graphical Macro Using Function Wizard

Note

For complete Function Wizard workflows, see Install and Use Function Wizard.

Create a macro that executes a graphical function on a Microsoft® Excel spreadsheet using the Function Wizard.

  1. Install and start the Function Wizard in Microsoft Excel. For details, see Install Function Wizard.

  2. Click Add. Select the function mysurf in the Functions for Class mysurfClass box.

  3. Click Add. The Function Properties dialog box appears.

  4. Click Done. Since mysurf.m does not have any inputs or outputs, there is no need to specify Properties.

    The Function Wizard Control Panel appears with mysurf selected in the list of Active Functions.

  5. In the Execute Functions area of the Function Wizard Control Panel, click Execute. The graphical output for mysurf appears in a separate window.

  6. Try interacting with the generated figure. For example, try dragging the figure window or inserting color bars and legends using the toolbar.

Save and Execute Macro

Once you are satisfied that your macro is usable, save the macro to execute it at your convenience.

  1. In the Function Wizard Control Panel, label the macro by entering mysurf in the Macro Name field of the Create Macros area.

  2. If desired, change the default value in the Store Macro In field.

  3. Click Create Macro.

  4. For details on executing macros, see Execute Macro in Excel. When the macro runs, you should see output similar to the surf peaks image above.

Create Macro That Displays Error Message

Create an add-in and macro using a function that displays an error message dialog box.

Create Add-In Using Function with Dialog Box

Build your COM component and add-in with MATLAB Compiler.

  1. Create a MATLAB function that displays a dialog box. For this example, create a function named myerror.m.

    function out = myerror(in)
    if (in < 0)
        error('Negative input not expected');
    else
        out = sqrt(in);
    end
  2. Build the Excel add-in using the myerror function with the Library Compiler app or compiler.build.excelAddIn.

    For example, if you are using compiler.build.excelAddIn, type:

    buildResults = compiler.build.excelAddIn('myerror.m', ...
    'GenerateVisualBasicFile','on');

    For more details, see the instructions in Create Excel Add-In from MATLAB.

Create Macro Using Function Wizard

Set arguments for the function using the Function Wizard.

  1. Install and start the Function Wizard in Microsoft Excel. For details, see Install Function Wizard.

  2. Click Add. Select the function myerror in the Functions for Class myerrorClass box.

  3. Click Add. The Function Properties dialog box appears.

  4. Add an argument with an input value of -1 to myerror.

    1. On the Inputs tab, click Set Input Data. The Input Data for in dialog box appears.

    2. Select Value and enter -1.

    3. Click Done.

  5. Define the output of myerror — in this case, choose any empty spreadsheet cell.

    1. Navigate to the Outputs tab and click Set Output Data. The Output Data for out dialog box appears.

    2. Select Range and select a spreadsheet cell value, C1, for example. The range field is automatically populated with Sheet1!$C$1.

    3. Click Done. The Function Wizard Control Panel appears with myerror selected in the list of Active Functions.

      Tip

      If you have functions besides myerror listed in the Active Functions list that you don't want to execute when you test myerror, deactivate these functions by selecting them and clicking Deactivate.

  6. Click Execute. The following dialog box is displayed.

Save and Execute Macro

Create a macro to display your error message on demand.

  1. In the Function Wizard Control Panel, label the macro by entering myerror in the Macro Name field of the Create Macros area.

  2. If desired, change the default value in the Store Macro In field.

  3. Click Create Macro.

  4. For details on executing macros, see Execute Macro in Excel.