MATLAB Answers

How to call a matlab function from an excel macro

54 views (last 30 days)
Aadil
Aadil on 15 Aug 2012
Commented: Harini Indla on 27 Nov 2019
Hi,
I'm creating an excel vba macro. I want to call and open matlab with a specific function by clicking the macro.
Is this possible?
Thanks,

  3 Comments

Aadil
Aadil on 16 Aug 2012
Dim MatLab As Object
Sub RunMatlabDayfiletool()
'Create the Matlab Object
Set MatLab = CreateObject("Matlab.Application")
Call MatLab.Execute("cd('D:\OLD')")
Call MatLab.Execute("AutoMakeFile.m")
End Sub
I ran this is vba, matlab opens up but doesn't run the script. When typing in 'cd' the path shown is the correct one
Can someone help with this?
Thanks
Harini Indla
Harini Indla on 27 Nov 2019
When I saw the example for this type of scripts in https://in.mathworks.com/help/matlab/ref/execute.html?searchHighlight=MatLab.Execute&s_tid=doc_srchtitle site I have observed that
Set Matlab = CreateObject("matlab.application")
but in your script it is different and try using "Feval" functionfor calling something in it.
for example check more about feval

Sign in to comment.

Accepted Answer

Aadil
Aadil on 21 Aug 2012
I can do basic things like plot graphs, create variables using this script but as soon as I tell it to run a script it freezes and excel starts giving this message:
'Microsoft Excel is waiting for another programme to complete an OLE Command'
This is the script I used:
Dim MatLab As Object
Sub RunMatlabDayfiletool()
'Create the Matlab Object
Set MatLab = CreateObject("Matlab.Application")
Result = MatLab.Execute("cd('\\Directory')")
Result = MatLab.Execute("x = -pi:.1:pi; y = sin(x); plot(x,y)")
End Sub

  1 Comment

Aadil
Aadil on 21 Aug 2012
OK looks like it runs the script but doesn't display the commands in the command window, everything runs invisibly
Any way round this?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!