Main Content

Create Macros with Graphical Functions

Create Macro that Displays Graphical Outputexample 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, seeCreate 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 withMATLAB®Compiler™

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

    functionmysurf surf(peaks);

  2. Build the Excel®add-in using themysurffunction with theLibrary Compilerapp orcompiler.build.excelAddIn

    For example, if you are usingcompiler.build.excelAddIn, type:

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

    For more details, see the instructions inCreate Excel Add-In from MATLAB

Create Graphical Macro Using Function Wizard

Note

For complete Function Wizard workflows, see安装和使用函数向导

Create a macro that executes a graphical function on aMicrosoft®Excelspreadsheet using the Function Wizard.

  1. Install and start the Function Wizard inMicrosoft Excel。For details, seeInstall Function Wizard

  2. ClickAdd。Select the functionmysurfin theFunctions for Class mysurfClassbox.

  3. ClickAdd。的Function Properties dialog box appears.

  4. ClickDone。Sincemysurf.mdoes not have any inputs or outputs, there is no need to specifyProperties

    的Function Wizard Control Panel appears withmysurfselected in the list ofActive Functions

  5. In the Execute Functions area of the Function Wizard Control Panel, click执行。的graphical output formysurf出现在一个单独的窗口。

  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 enteringmysurfin theMacro Namefield of the Create Macros area.

  2. If desired, change the default value in theStore Macro Infield.

  3. ClickCreate Macro

  4. For details on executing macros, see执行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 withMATLAB Compiler

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

    functionout = myerror(in)if(in < 0) error('Negative input not expected');elseout = sqrt(in);end
  2. Build the Excel add-in using themyerrorfunction with theLibrary Compilerapp orcompiler.build.excelAddIn

    For example, if you are usingcompiler.build.excelAddIn, type:

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

    For more details, see the instructions inCreate 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 inMicrosoft Excel。For details, seeInstall Function Wizard

  2. ClickAdd。Select the functionmyerrorin theFunctions for Class myerrorClassbox.

  3. ClickAdd。的Function Properties dialog box appears.

  4. Add an argument with an input value of -1 tomyerror

    1. On theInputstab, clickSet Input Data。的Input Data for indialog box appears.

    2. SelectValueand enter-1

    3. ClickDone

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

    1. Navigate to theOutputstab and clickSet Output Data。的Output Data for outdialog box appears.

    2. SelectRangeand select a spreadsheet cell value,C1, for example. The range field is automatically populated withSheet1!$C$1

    3. ClickDone。的Function Wizard Control Panel appears withmyerrorselected in the list ofActive Functions

      Tip

      If you have functions besidesmyerrorlisted in theActive Functionslist that you don't want to execute when you testmyerror, deactivate these functions by selecting them and clickingDeactivate

  6. Click执行。的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 enteringmyerrorin theMacro Namefield of the Create Macros area.

  2. If desired, change the default value in theStore Macro Infield.

  3. ClickCreate Macro

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