Running a specific m-file/fig from excel

13 Ansichten (letzte 30 Tage)
Johan
Johan am 2 Jun. 2011
Bearbeitet: John Kelly am 3 Jun. 2014
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.

Akzeptierte Antwort

Johan
Johan am 2 Jun. 2011
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 Kommentare
Walter Roberson
Walter Roberson am 2 Jun. 2011
Dang, my first version didn't have them and then I "corrected" it. Ah well.
Johan
Johan am 2 Jun. 2011
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (5)

Chirag Gupta
Chirag Gupta am 2 Jun. 2011
Bearbeitet: John Kelly am 3 Jun. 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 Kommentare
Walter Roberson
Walter Roberson am 2 Jun. 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
Johan am 2 Jun. 2011
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...

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 2 Jun. 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 Kommentare
Walter Roberson
Walter Roberson am 2 Jun. 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
Johan am 2 Jun. 2011
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.

Melden Sie sich an, um zu kommentieren.


Johan
Johan am 2 Jun. 2011
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 Kommentare
Johan
Johan am 2 Jun. 2011
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 am 2 Jun. 2011
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.

Melden Sie sich an, um zu kommentieren.


Johan
Johan am 2 Jun. 2011
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 Kommentare
Walter Roberson
Walter Roberson am 2 Jun. 2011
cdsDir = "cd('" & sDir & "')"
hMatlab.Execute (cdsDir)
Johan
Johan am 2 Jun. 2011
tried something like that too, and just tried yours as well, not quite working though. sadly.

Melden Sie sich an, um zu kommentieren.


Johan
Johan am 3 Jun. 2011
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 Kommentare
Walter Roberson
Walter Roberson am 3 Jun. 2011
Sorry, I don't have experience with that.
Aadil
Aadil am 16 Aug. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Export to MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by