Anda di halaman 1dari 2

TO RUN MATLAB COMMANDS OR FILES FROM

WITHIN POWERPOINT:
Here are 5 simple steps that will have you executing your MATLAB code from
PowerPoint in no time!

1) Enable MATLAB as an automation server. In MATLAB:

enableservice('automationserver',true);

Note: I have this command in my startup.m file, so MATLAB-as-automation-server is


always enabled.

2) Within PowerPoint, launch the Visual Basic editor, create a module, and paste this
function in it:

%%%

Sub RunFile(FileName As String, Optional FilePath As String)


Dim MATLAB As Object
Dim Result As String
Dim Command As String
Dim MATLABWasNotRunning As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''''
' Set Up
'''''''''''''''''''''''''''''''''''''''''''''''''''
' Connect to the automation server.
' MATLAB becomes a MATLAB handle to the running instance
' of MATLAB.
On Error Resume Next ' Defer error trapping
MATLAB = GetObject(, "matlab.application")
If Err.Number <> 0 Then
MATLABWasNotRunning = True
Set MATLAB = CreateObject("matlab.application")
Err.Clear ' Clear Err object in case error occurred.
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''
' Do the Work
'''''''''''''''''''''''''''''''''''''''''''''''''''
If Not IsMissing(FilePath) And Not FilePath = "" Then
Command = "cd('" + FilePath + "')"
Result = MATLAB.Execute(Command)
End If
Command = FileName
Result = MATLAB.Execute(Command)
'Result = MsgBox("Done", vbOkayOnly, "Click when done")
End Sub

%%%
3) To call a file in MATLAB, paste a custom subroutine underneath the Sub RunFile
code, of the following format:

Sub FILENAME()
Call RunFile("COMMANDS TO RUN", "DIRECTORY TO SWITCH TO")
End Sub

For instance, let’s assume that we want to run a file entitled assayTool that resides in
"C:\MFILES\Assay Tool". Moreover, we want to issue a clear all;close
all;clc; before we run it, and we want to change directories to that containing
AssayTool.

We would create a subroutine reading:

Sub RunAssayTool()
Call RunFile("clear all;close all;clc;assayTool", "C:\MFILES\Assay
Tool")
End Sub

4) Link the subroutine to some object, picture, text, etc. that you want to use to trigger the
behavior in PowerPoint. (Right-click on the object, select Action Settings, and from the
“Mouse Click” tab, select “Run Macro.” Browse to select from the module the subroutine
RunAssayTool.

5) BEST PRACTICE SUGGESTION (STRONGLY ENCOURAGED): I keep a single


text document called RunFilesMain.bas that contains my RunFile() code AND ALL OF
MY INDIVIDUAL SUBROUTINES. When I want to add a new link to a MATLAB file
from PowerPoint, I simply edit RunFilesMain.bas to create the appropriate subroutine,
delete the existing VB module in the PowerPoint (Open the VB editor, click to activate
the existing module, then File -> Remove Module. You can save it if you want; I
typically don’t.) Then simply import the newly modified version of the .BAS file back
into the PowerPoint. (TOOLS -> MACROS -> VisualBasic Editor; then FILE ->
IMPORT… browse to RunFilesMain.bas.) The file will be installed as a new module, and
then, right-clicking on an object in PowerPoint, you can select the appropriate subroutine
to run from a listbox.

I ALWAYS only edit that single .BAS file. If I want to update the macros in a given
PowerPoint, I delete the current module, modify RunFilesMain.bas, and reimport it. All
existing links to subroutines will be automatically re-established (unless you’ve changed
the name of the subroutine.) This greatly simplifies management of the VB routines.

Anda mungkin juga menyukai