Running a specific m-file/fig from excel

조회 수: 5 (최근 30일)
Johan
Johan 2011년 6월 2일
편집: John Kelly 2014년 6월 3일
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.

채택된 답변

Johan
Johan 2011년 6월 2일
Ah found the little mistake in our codes, the correct code:
Private Sub afstand()
Dim hMatlab As Object
Dim sDir As String, cdsDir As String, s1 As String
Dim Result As String
Set hMatlab = CreateObject("matlab.application")
s1 = "'"
sDir = s1 & ActiveWorkbook.path & s1
cdsDir = "cd(" & sDir & ")"
hMatlab.Execute (cdsDir)
hMatlab.Execute ("importexcel")
Result = hMatlab.Execute("afstand(1,2)")
MsgBox Result
End Sub
needed to remove the ' sign from the cdsDir, that's all
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 6월 2일
Dang, my first version didn't have them and then I "corrected" it. Ah well.
Johan
Johan 2011년 6월 2일
Yeah...often the little things that change the whole thing, Really great help though, couldn't have done it without you guys, or maybe I could, but would take me days to figure it out my self.

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Chirag Gupta
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
Walter Roberson
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
Johan 2011년 6월 2일
okay, well it seems you are right, now all I need to figure out is how to get matlab to run one of my functions and return its result in a msgbox or something like that... any thoughts? seems easy enough, just can't seem to get it to work, yet...

댓글을 달려면 로그인하십시오.


Walter Roberson
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
Walter Roberson
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
Johan 2011년 6월 2일
yeah...so far not working, maybe it's bacause it only works with access 2003? but still, should be the same though, but will try to work around with the syntax, altough must say if I could get the com server thing to work that might be even better than all this shell-stuff, just thought I needed the spreadsheet EX link to do that, but if I could use com to execute a function in matlab and then return the result to excel that would be optimal.

댓글을 달려면 로그인하십시오.


Johan
Johan 2011년 6월 2일
okay, seems to work, to some degree, for instance I can do something like result=hmatlab.execute(1+2) msgbox result
which returns ans = 3
but say I need to run my functions, I first have a function that needs to be run, which imports the data from excel, and then after that I can call other commands, how would I go about doing that?
and do I need to set some kind of path, since my functions are in a folder inside the matlab folder in documents like before.
  댓글 수: 3
Johan
Johan 2011년 6월 2일
YES!!!
it works like a charm...so far...hehe, will see if I can make it work for all the functions in the project.
Johan
Johan 2011년 6월 2일
any thoughts on how to replace the path in the .execute("cd('path')") with a string? if possible, it's because I need to first have excel determine the path, which I have done, and then use that path when it executes.

댓글을 달려면 로그인하십시오.


Johan
Johan 2011년 6월 2일
am doing this:
Private Sub afstand()
Dim hMatlab As Object
Dim sDir As String, s1 As String
Dim Result As String
Set hMatlab = CreateObject("matlab.application")
s1 = "'"
sDir = s1 & ActiveWorkbook.path & s1
hMatlab.Execute ("cd(sDir)")
hMatlab.Execute ("importexcel")
Result = hMatlab.Execute("afstand(1,2)")
MsgBox Result
End Sub
the thing not working is the part where I get matlab to go to sDir
any thoughts on what I am doing wrong?
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 6월 2일
cdsDir = "cd('" & sDir & "')"
hMatlab.Execute (cdsDir)
Johan
Johan 2011년 6월 2일
tried something like that too, and just tried yours as well, not quite working though. sadly.

댓글을 달려면 로그인하십시오.


Johan
Johan 2011년 6월 3일
okay small problem.
I have a GUI that I open like this from excel:
Sub openmatlab()
Dim hMatlab As Object
Dim sDir As String, scdDir As String, s1 As String
Dim result As Variant
Set hMatlab = CreateObject("matlab.application")
s1 = "'"
sDir = s1 & ActiveWorkbook.path & s1
scdDir = "cd(" & sDir & ")"
hMatlab.Execute (scdDir)
hMatlab.Execute ("importexcel")
result = hMatlab.Execute("starter")
mlpStart.Value = 0
End Sub
but it shuts down the GUI almost right after it opened, how do I keep it open until the user presses close?
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 6월 3일
Sorry, I don't have experience with that.
Aadil
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Export to MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by