Running a specific m-file/fig from excel
이전 댓글 표시
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.
채택된 답변
추가 답변 (5개)
Chirag Gupta
2011년 6월 2일
편집: John Kelly
2014년 6월 3일
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
Johan
2011년 6월 2일
Walter Roberson
2011년 6월 2일
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
2011년 6월 2일
Johan
2011년 6월 2일
Walter Roberson
2011년 6월 2일
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
2011년 6월 2일
Walter Roberson
2011년 6월 2일
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
Johan
2011년 6월 2일
Johan
2011년 6월 2일
Walter Roberson
2011년 6월 2일
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
2011년 6월 2일
Walter Roberson
2011년 6월 2일
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
2011년 6월 2일
Johan
2011년 6월 2일
0 개 추천
댓글 수: 3
Walter Roberson
2011년 6월 2일
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
2011년 6월 2일
Johan
2011년 6월 2일
Johan
2011년 6월 2일
댓글 수: 2
Walter Roberson
2011년 6월 2일
cdsDir = "cd('" & sDir & "')"
hMatlab.Execute (cdsDir)
Johan
2011년 6월 2일
Johan
2011년 6월 3일
댓글 수: 2
Walter Roberson
2011년 6월 3일
Sorry, I don't have experience with that.
Aadil
2012년 8월 16일
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.
카테고리
도움말 센터 및 File Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!