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™。
Create a MATLAB function with a graphical output. For this example, create a function named
mysurf.m
。functionmysurf surf(peaks);
Build the Excel®add-in using the
mysurf
function with theLibrary Compilerapp 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 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.
Install and start the Function Wizard inMicrosoft Excel。For details, seeInstall Function Wizard。
ClickAdd。Select the functionmysurfin theFunctions for Class mysurfClassbox.
ClickAdd。的Function Properties dialog box appears.
ClickDone。Since
mysurf.m
does not have any inputs or outputs, there is no need to specifyProperties。的Function Wizard Control Panel appears with
mysurf
selected in the list ofActive Functions。In the Execute Functions area of the Function Wizard Control Panel, click执行。的graphical output for
mysurf
出现在一个单独的窗口。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 theMacro Namefield of the Create Macros area.If desired, change the default value in theStore Macro Infield.
ClickCreate Macro。
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。
Create a MATLAB function that displays a dialog box. For this example, create a function named
myerror.m
。functionout = myerror(in)if(in < 0) error('Negative input not expected');elseout = sqrt(in);end
Build the Excel add-in using the
myerror
function with theLibrary Compilerapp 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 inCreate 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 inMicrosoft Excel。For details, seeInstall Function Wizard。
ClickAdd。Select the function
myerror
in theFunctions for Class myerrorClassbox.ClickAdd。的Function Properties dialog box appears.
Add an argument with an input value of -1 to
myerror
。On theInputstab, clickSet Input Data。的Input Data for indialog box appears.
SelectValueand enter
-1
。ClickDone。
Define the output of
myerror
— in this case, choose any empty spreadsheet cell.Navigate to theOutputstab and clickSet Output Data。的Output Data for outdialog box appears.
SelectRangeand select a spreadsheet cell value,
C1
, for example. The range field is automatically populated withSheet1!$C$1
。ClickDone。的Function Wizard Control Panel appears with
myerror
selected in the list ofActive Functions。Tip
If you have functions besides
myerror
listed in theActive Functionslist that you don't want to execute when you testmyerror
, deactivate these functions by selecting them and clickingDeactivate。
Click执行。的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 theMacro Namefield of the Create Macros area.If desired, change the default value in theStore Macro Infield.
ClickCreate Macro。
For details on executing macros, see执行Macro in Excel。