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 Center および File ExchangeData Export to MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by