Here is a simple example demonstrating:
- How to call methods written in C#/.NET from matlab
- How to pass scalar and array parameters from matlab to C#/.NET methods
1) Write methods in C#/.NETWe will call the simple mathematical functions
Transpose and
Square below from matlab.
Transpose() requires a 2-dimensional array (matrix, of type double[,]), Square() requires a scalar number (of type double) as input parameter.
- Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
using System;
using System.Collections.Generic;
using System.Text;
namespace Call_NETfromMatlab
{
public class MathLib
{
// Transpose matrix
public static double[,] Transpose(double[,] M)
{
if (M == null || M.Length == 0)
throw new Exception
("Null-valued or empty input matrix M!");
try
{
// get row and column sizes
int row_count = M.GetUpperBound(0) + 1;
int col_count = M.GetUpperBound(1) + 1;
// create output matrix
double[,
] P
= new double[col_count, row_count
];
// fill values
for (int i = 0; i < row_count; i++)
{
for (int j = 0; j < col_count; j++)
{
P[j,i] = M[i,j];
}
}
return P;
}
catch(Exception ex)
{
throw new Exception
("MatrixFunc.Transpose: " + ex
.Message); }
}
// Return square of a number
public static double Square(double x)
{
return x * x;
}
}
}
- GeSHi ©
2) Compile .NET code to obtain the assembly (dll file) Call_NETfromMatlab.dllWe will load this .NET assembly in matlab to access its method from matlab
3) Write and execute matlab scriptNote how .NET assembly
Call_NETfromMatlab.dll is loaded
NET.addAssembly with to access its methods.
- Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
% create .NET array
A = [1 2 3; 4 5 6]
% path to dll assembly (replace it with your own path)
assemblypath = 'C:\Users\finaquant\Documents\...\Call_NETfromMatlab.dll';
% load .NET matrix function library
NET.addAssembly(assemblypath);
% call methods
MathLib = Call_NETfromMatlab.MathLib;
% square scalar number
y = MathLib.Square(100)
% transpose matrix
% http://www.mathworks.ch/ch/help/matlab/ref/net.convertarray.html
P = MathLib.Transpose(NET.convertArray(A,'System.Double'));
% convert .NET array to matlab array
% http://www.mathworks.com/matlabcentral/answers/46827
- GeSHi ©
As the example above demonstrates, you can call .NET methods from matlab. It is also quite easy to pass parameters from matlab to .NET methods.
This means, it is possible to connect matlab with the Calculation Engine
finaquant® calcs for simulations.

You can first implement a complex cause-effect relationship with finaquant® calcs, and use matlab for running this Calculation Engine with varied inputs (value drivers), and presenting the results visually with graphs, tables etc.
This simulation capability makes all sort of analytical evaluations possible: Scenario analysis, estimations & predictions, multi-dimensional optimizations..