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™.
Create a MATLAB function with a graphical output. For this example, create a function named
mysurf.m
.function mysurf surf(peaks);
Build the Excel® add-in using the
mysurf
function with the Library Compiler app orcompiler.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.
Install and start the Function Wizard in Microsoft Excel. For details, see Install Function Wizard.
Click Add. Select the function mysurf in the Functions for Class mysurfClass box.
Click Add. The Function Properties dialog box appears.
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.In the Execute Functions area of the Function Wizard Control Panel, click Execute. The graphical output for
mysurf
appears in a separate window.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.
In the Function Wizard Control Panel, label the macro by entering
mysurf
in the Macro Name field of the Create Macros area.If desired, change the default value in the Store Macro In field.
Click Create Macro.
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.
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
Build the Excel add-in using the
myerror
function with the Library Compiler app orcompiler.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.
Install and start the Function Wizard in Microsoft Excel. For details, see Install Function Wizard.
Click Add. Select the function
myerror
in the Functions for Class myerrorClass box.Click Add. The Function Properties dialog box appears.
Add an argument with an input value of -1 to
myerror
.On the Inputs tab, click Set Input Data. The Input Data for in dialog box appears.
Select Value and enter
-1
.Click Done.
Define the output of
myerror
— in this case, choose any empty spreadsheet cell.Navigate to the Outputs tab and click Set Output Data. The Output Data for out dialog box appears.
Select Range and select a spreadsheet cell value,
C1
, for example. The range field is automatically populated withSheet1!$C$1
.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 testmyerror
, deactivate these functions by selecting them and clicking Deactivate.
Click Execute. The following dialog box is displayed.
Save and Execute Macro
Create a macro to display your error message on demand.
In the Function Wizard Control Panel, label the macro by entering
myerror
in the Macro Name field of the Create Macros area.If desired, change the default value in the Store Macro In field.
Click Create Macro.
For details on executing macros, see Execute Macro in Excel.