How do you keep a MATLAB GUI open when launched from Excel VBA?

6 views (last 30 days)
I have a MATLAB GUI built using the guide tool. I wish for it to be launched externally from an Excel VBA macro. The code I have currently launches it, but it instantly closes as soon as it has finished launching and opening.
Is there any way to keep the GUI from closing automatically?
Sub RunMATLABGUI()
Dim MatLab As Object
Dim result As String
Dim Flocation As String
Flocation = ThisWorkbook.Path
Dim hMatlab As Object
Set hMatlab = CreateObject("Matlab.Application")
result = hMatlab.Execute("cd " & Flocation)
result = hMatlab.Execute("run ResultsGUI_For_MultiYrs_Sheet.m")
'This was the old code. It worked, but required a direct link to the MATLAB executible
'ChDir Application.ActiveWorkbook.Path
'Shell ("""C:\Program Files\MATLAB\R2017b\bin\matlab.exe"" -nodisplay -nosplash -nodesktop -r ""run('ResultsGUI_For_MultiYrs_Sheet.m');""")
End Sub
  3 Comments
Guillaume
Guillaume on 6 Feb 2018
I've just tested your code with a simple GUI (the default GUI with UI controls autogenerated by guide) and the GUI stays up and I can interact with it with no issues.
Therefore I suspect that your GUI closes because of a bug with it. Possibly, the slightly different environment that you get when starting the matlab COM server causes your gui to crash.
Matthew Weis
Matthew Weis on 6 Feb 2018
I thought that may be the case too. When I run it with MATLAB open, however, it has no problem, which led me to believe it was the way in which I was launching my GUI that was at fault.

Sign in to comment.

Answers (0)

Categories

Find more on Data Export to MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!