MATLAB Answers

Yale
0

Executing Matlab.m script from Excel VBA macro

Asked by Yale
on 6 Nov 2012
Latest activity Answered by Antonio Gugin on 29 Jul 2019
The Matlab code that we wish to run reads in an “input.xls” file, performs calculations in Matlab .m modules, then outputs results to an “output.xls” file. There is an Excel VBA macro in a separate workbook that populates the “input.xls” file before calling the Matlab .m file with the following code:
Dim MatLab As Object
Set MatLab = CreateObject("Matlab.Application")
MatLab.Execute (PATH TO MATLAB.m)
The Matlab code should then populate the “output.xls” file (using xlswrite functions) with the final results once the run is complete. The Matlab code now fails to write to the “output.xls” file. There is evidence that the Excel macro is successfully calling the Matlab code since the Matlab application is opening in the background and running for the expected amount of time. However, the instance of Matlab is not visible and without the “output.xls” file, there is no way to diagnose the potential errors. Also, please note that run from the Matlab command line, the Matlab code populates the “output.xls” file perfectly so there should be no issues with the Matlab code.
Any help would be much appreciated!

  2 Comments

Not really answering your question here, but why not use MATLAB to do the populating of the input.xls spreadsheet rather than using VBA? Or, why not ditch Excel completely and do it all in MATLAB? I'm sure you have good reasons for doing it the way you are but I'm just curious.
Time management is zero sum. If the VBA already exists, then using it enables you to do something else.

Sign in to comment.

5 Answers

Answer by Aaron Close on 22 Feb 2017

you can use in VBA
fileToRun = "C:\path\path\file.m"
matlabCommand = "matlab -nodisplay -nosplash -nodesktop -r "" run('" & fileToRun & "');exit;"" "
Shell (matlabCommand)

  1 Comment

Hi, could you help me please. I run my macro whit the code, but I don't have response
Function call_r() As Double
fileToRun = "C:\Users\Dell\OneDrive\Documentos2\MATLAB\suma.m"
matlabCommand = "matlab -nodisplay -nosplash -nodesktop -r "" run('" & fileToRun & "');exit;"" "
call_r = Shell(matlabCommand)
End Function
The code in Matlab:
function [res] = suma()
num1 = 7;
num2 = 5;
res = num1 + num2;
end

Sign in to comment.


Answer by Vishal Rane on 8 Nov 2012

Have you explored the Spreadsheet Link EX toolbox ?
From its description I think it might help.

  0 Comments

Sign in to comment.


Answer by Karabo Magoro on 13 Dec 2012

Hi Yale, I'm also having the same problem. If you find/found the solution please share.
Regards, Karabo

  0 Comments

Sign in to comment.


Answer by Long Cheng on 25 May 2016

Hi, Yale,
I am having similar problem. Please share if you have any good solutions. Thank you.

  0 Comments

Sign in to comment.


Answer by Antonio Gugin on 29 Jul 2019

Hey guys,
I am using this very helpful code (Thanks a lot), however, I was wondering if there was a way to use an already opened matlab window (if there is such) instead of always opening a new one each time this is ran. Thanks a lot!
fileToRun = "C:\path\path\file.m"
matlabCommand = "matlab -r "" run('" & fileToRun & "');"" "
Shell (matlabCommand)

  0 Comments

Sign in to comment.