- Joined
- 5/2/06
- Messages
- 12,532
- Points
- 273
Some people use Matlab and/or Excel VBA at work and there is generally a desire to take advantage of Matlab many built in functions. With little effort, we can also call VBA functions from Matlab.
Call Matlab functions from Excel/VBA
The most convinient way to perform this is to use Excel Link which is an Excel addin component that comes with Matlab. Once you install Excel Link, it will expose all Matlab functions to Excel/VBA that you can use as spreadsheet formula.
The way to call Matlab function is via matlabfnc(). Matlabfcn() takes as input arguments the name of the Matlab function as string, followed by the input arguments of the Matlab function.
This would give a quick and simple way to use many of Matlab nice operations (matrix, stat). However, there will be some big performance penalty if you load the spreadsheet with many matlabfnc() as each will call out to Matlab and back individually.
Call VBA functions from Matlab
This is a very little discussed topic that may come up in rare cases. It's useful if you have much of your code base in Matlab and want to call some customized function that you have stuck away in some other XLS sheet.
1. Excel/VBA
You have Book1.xls in C:\Code\ folder that contains a VBA function named Func1 with this simple code
2. Matlab
Type these commands into the editor
Call Matlab functions from Excel/VBA
The most convinient way to perform this is to use Excel Link which is an Excel addin component that comes with Matlab. Once you install Excel Link, it will expose all Matlab functions to Excel/VBA that you can use as spreadsheet formula.
The way to call Matlab function is via matlabfnc(). Matlabfcn() takes as input arguments the name of the Matlab function as string, followed by the input arguments of the Matlab function.
This would give a quick and simple way to use many of Matlab nice operations (matrix, stat). However, there will be some big performance penalty if you load the spreadsheet with many matlabfnc() as each will call out to Matlab and back individually.
Call VBA functions from Matlab
This is a very little discussed topic that may come up in rare cases. It's useful if you have much of your code base in Matlab and want to call some customized function that you have stuck away in some other XLS sheet.
1. Excel/VBA
You have Book1.xls in C:\Code\ folder that contains a VBA function named Func1 with this simple code
2. Matlab
Type these commands into the editor
Code:
%Open a COM server on Matlab
Excel = actxserver('Excel.Application');
Workbook = Excel.Workbooks.Open('c:\Code\Book1.xls');
x = fseminf(@(x) callFunc1(x,Excel),x0,ntheta,seminfcon);
function x = callFunc1(param,Excel)
x=Excel.Run('Func1',param);
Excel.Quit;