calling matlab functions from .NET?

Table-valued functions in finaquant .net libraries

calling matlab functions from .NET?

Postby selmar » 17 Nov 2013, 18:32

Hello,
Some of finaquant's table functions like function router are quite interesting for our financial applications. We have some complex matlab routines for risk and performance calculations. Is it possible to call matlab functions from such table functions?

You have an example for calling R functions
http://finaquant.com/calling-user-defin ... hin-c/3060

Regards
Selena
selmar
 
Posts: 11
Joined: 19 Sep 2013, 19:26

Calling a matlab function from within C#/.NET

Postby finaquant » 22 Nov 2013, 12:20

Yes, it is possible to call matlab functions from Table Functions like Subtable Transformer, or Function Router of our .NET libraries finaquant® protos (non-commercial) or finaquant® calcs (commercial).

You need to compile matlab functions first into .NET dll's using the deploytool of matlab. You can then reference this newly created dll, plus MWarray.dll of matlab in order to call matlab functions from within .NET
Image

The example below will make it clear: Calling a simple matlab function from within C#/.NET

1) Write a simple function addMatrices.m in matlab and save it. We will call this function from C#/.NET
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. function [ c ] = addMatrices( a,b )
  2. c=a+b; %add the matrices.
  3. end
GeSHi ©


2) Open Deployment Tool (deploytool) in matlab and add the function file addMatrices.m to the package named MatCode.prj
There can be other functions in the package like AddSubMatrices.m and PlotVectorPair.m

3) Add a class to deploy package; in this example MyMatClass This class will contain our matlab functions as .NET methods
Image

4) Build the package MatCode.prj The generated MatCode.dll will be referenced in our Visual Studio project.

5) Add matlab-related references (dll's) to Visual Studio project as shown with red arrows in the picture above: MatCode.dll and MWarray.dll

MWarray.dll can be found under a folder like:
\Program Files\MATLAB\MATLAB Compiler Runtime\v...\toolbox\dotnetbuilder\bin\win64\v...\MWArray.dll

6) Write the C#/.NET method which calls the matlab function:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. static public void SimpleMatrixAddition()
  2. {
  3.     double[,] a = { { 2, 3 }, { 5, 6 }, { 8, 9 } }; //Matrix 1
  4.     double[,] b = { { 1, 2}, { 4, 5}, { 7, 8} };            //Matrix 2
  5.    
  6.     MWNumericArray arr1 = a;
  7.     MWNumericArray arr2 = b;
  8.    
  9.     MyMatClass obj = new MyMatClass();
  10.  
  11.     // call matlab function
  12.     MWArray result = (MWNumericArray)obj.addMatrices((MWArray)arr1, (MWArray)arr2);
  13.  
  14.     // display matlab matrix
  15.     Console.WriteLine("matlab matrix:\n" + result);
  16.     Console.ReadKey();
  17. }
GeSHi ©


7) Build and run C#/.NET method
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. namespace MatlabDotNETintegration
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Test.SimpleMatrixAddition();
  8.         }
  9.     }
  10. }
GeSHi ©


8) Outcome:
Image
User avatar
finaquant
 
Posts: 61
Joined: 16 Sep 2013, 18:37

Useful links for matlab - DotNet integration

Postby finaquant » 22 Nov 2013, 12:36

Useful links for matlab - .Net integration:

Accessing MATLAB functions from C#.NET
http://domoreinlesstime.wordpress.com/2 ... ab-from-c/

How To Call Functions In a MATLAB Generated .NET DLL From C#
http://scriptbucket.wordpress.com/2012/ ... ll-from-c/

Using C# functions and forms in Matlab
http://www.mathworks.com/matlabcentral/ ... -in-matlab

How can I extract a single native element from an MWArray in MATLAB Builder for .NET 2.1 (R2006b) without using a temporary native array in C#?
http://www.mathworks.com/matlabcentral/answers/97372

Is it possible to use .NET application in MatLab?
http://stackoverflow.com/questions/1448 ... -in-matlab
User avatar
finaquant
 
Posts: 61
Joined: 16 Sep 2013, 18:37

Re: calling matlab functions from .NET?

Postby selmar » 24 Nov 2013, 22:02

it works, thanks. Selina
selmar
 
Posts: 11
Joined: 19 Sep 2013, 19:26


Return to Table Functions

Who is online

Users browsing this forum: No registered users and 1 guest

cron