Running a specific m-file/fig from excel
Mostra commenti meno recenti
Hi all!
I have the student version of matlab, so no extended functionality between excel and matlab sadly.
My "problem" is that I would like to run a specific m-file or .fig either way, it's my GUI that needs to be run, using VBA in excel.
I know I can open matlab by using shell, but I tried adjusting the command so that it would run the m-file, or the .fig, using '-r', it didn't really work, maybe because when I use shell to open matlab it doesn't open the program with the MATLAB-folder open, it opens with the documents-folder open, which is like one level up from MATLAB-folder.
so how do I use VBA to run a file that is located at C:\Users\username\Documents\MATLAB\MontageInc\?
This is what I did so far:
Sub openmatlab()
Dim vRun As Variant
vRun = Shell("matlab.exe")
End Sub
also tried including the -r scriptname.m in the shell-command, which doesn't work.
If anyone knows this I would be very happy.
Risposta accettata
Più risposte (5)
Chirag Gupta
il 2 Giu 2011
Modificato: John Kelly
il 3 Giu 2014
There are lots of options:
You should look at MATLAB as a COM Automation Server.
You should be able to start MATLAB from VBA and then execute MATLAB functions.
Before you do that, just register MATLAB as a COM server:
You can run this command on MATLAB Command prompt: enableservice('AutomationServer',true)
Then a VBA program like:
Sub runMatlab()
Dim hMatlab As Object
Set hMatlab = CreateObject("Matlab.Application")
hMatlab.Execute ("surf(peaks)")
End Sub
You can execute multiple commands etc.
6 Commenti
Johan
il 2 Giu 2011
Walter Roberson
il 2 Giu 2011
Well you could start with something akin to
Shell("matlab.exe -r \"try;enableservice('AutomationServer',true);catch;end;quit\"")
or even skip the try/catch if you are bold,
Shell("matlab.exe -r \"enableservice('AutomationServer',true);quit\"")
and then you would have COM server access to MATLAB to be able to do more complex things.
Johan
il 2 Giu 2011
Johan
il 2 Giu 2011
Walter Roberson
il 2 Giu 2011
That's a good question; it appears to me that Spreadsheet Link EX would not be needed. See http://www.mathworks.com/help/techdoc/matlab_external/brd0vd4-1.html
The stuff about existing automation servers leads me to wonder if it might be necessary to not Quit from the started automation server and perhaps using & at the end of the Shell command.
This is not a topic I have looked at before, and I do not have a Windows box to try it with.
Johan
il 2 Giu 2011
Walter Roberson
il 2 Giu 2011
Something like,
matlab.exe -r "try run('C:\Users\username\Documents\MATLAB\MontageInc\scriptname.m');catch;end;quit"
But only if scriptname really is a script. Otherwise,
matlab.exe -r "cd('C:\Users\username\Documents\MATLAB\MontageInc');try scriptname;catch;end;quit"
6 Commenti
Johan
il 2 Giu 2011
Johan
il 2 Giu 2011
Walter Roberson
il 2 Giu 2011
The " will need to appear in the Shell command. I do not know the syntax for that for VB. Possibly
vRun = Shell("matlab.exe -r \"try run('MATLAB\MontageInc\starter.m');catch;end;quit\"")
Johan
il 2 Giu 2011
Walter Roberson
il 2 Giu 2011
http://msdn.microsoft.com/en-us/library/aa212167%28v=office.11%29.aspx
Seems to imply it isn't as easy as in most other languages. Anyhow, a solution is given there.
Johan
il 2 Giu 2011
Johan
il 2 Giu 2011
0 voti
3 Commenti
Walter Roberson
il 2 Giu 2011
Did you double-check the execution with
result=hmatlab.execute("1+2")
as otherwise the 1+2 would be executed in VB ?
Once you have a quoted string working for expressions, you should be able to .execute("cd('C:\Users\username\Documents\MATLAB\MontageInc')")
and then .execute("scriptname")
Johan
il 2 Giu 2011
Johan
il 2 Giu 2011
Johan
il 2 Giu 2011
2 Commenti
Walter Roberson
il 2 Giu 2011
cdsDir = "cd('" & sDir & "')"
hMatlab.Execute (cdsDir)
Johan
il 2 Giu 2011
Johan
il 3 Giu 2011
2 Commenti
Walter Roberson
il 3 Giu 2011
Sorry, I don't have experience with that.
Aadil
il 16 Ago 2012
This bit of script is the problem:
Dim hMatlab As Object
I've been trying to execute matlab from vba as well and I notice when ever the dim matlab as object code is put right at the beggining it works, for instance with chirags scripts I moved it to the top and the figure remained open:
Dim hMatlab As Object
Sub runMatlab()
Set hMatlab = CreateObject("Matlab.Application")
hMatlab.Execute ("surf(peaks)")
End Sub
I know I'm a bit late sorry.
Categorie
Scopri di più su Use COM Objects in MATLAB in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!