C # Matlab calls each other
Posted on 2016/7/28 17:21:42 3551 people read
Category: mixed programming
C # Matlab calls each other
Test environment
VisualStudio2013/. net4.0
matlab2015b
The higher version of Matlab is easy to connect to other languages and does not require a bunch of configuration.
Other languages interact with Matlab in a similar operation.
C # calls Matlab
Basic idea: The matlab function is packaged into DLL files, combined with MATLAB data support DLL (MWArray.dll), delivered to other language programs.
1,matlab side of the Operation
Write matlab functions:
function [result,m,n] = Getselfmultiplyresult (list)
% the product of the computed matrix and its transpose matrix
% test returns multiple results
result = List*list ' [m,n] = size end
· function result = Getselfsquareresult(list)
The result of calculating the square of each element of the matrix
result = list. ^2;
End
· Packaging functions:
· Locate the Library compiler (librarycompiler)
· Packaging functions
As shown,
1) Select the target type;
2) Add the function files that need to be packaged;
3) Rename the library name.
3. Rename the class name, or add a class that assigns the class to which the function belongs. Complete the packaging operation.
Export DLL
In the generated file, locate the "for_redistribution_files_only" folder, which contains
CalcMatResult.dll CalcMatResultNative.dll two dll files, all can be used.
In addition,MWArray.dll in the matlab installation directory, refer to the path:
X:\program Files\matlab\r2015b\toolbox\dotnetbuilder\bin\win\v2.0\
· 1
You can also directly search directly using software such as everything.
2,C # End of the Operation
The C # end uses the two DLL files of MWArray.dll and CalcMatResultNative.dll .
· Add DLL Reference
· Demo Code
Using System;
Using calcmatresultnative; //Add Reference
Using MathWorks.MATLAB.NET.Arrays; //Add Reference
namespacecsharpmatlabdemo
{
Span lang= "en-US" >class program
{
static void Main (string[ ] args
{
int[,] list ={{< Span lang= "en-US" >1},{2},{3},{4}}; //column vector
Mwarray Array = newmwnumericarray (list);
Calcmatresultnative.multiply multi= New Multiply ();
Object Resultobj =multi. Getselfmultiplyresult (3, array); 3 indicates the number of results returned, less than or equal to the actual number of return values of the MATLAB corresponding function
Object[] Resultobjs = (object[]) resultobj;
Double[,] calcresult= (double[,]) resultobjs[0];
double[,] Sizem = (double[,]) resultobjs[1];
double[,] Sizen = (double[,]) resultobjs[2];
Console.readkey ();
}
}
}
MATLAB calls C #
MATLAB calls C # more simple, First C # code compiled into a DLL library,matlab Direct reference can be called.
If it fails, check to see if the . NET version used is too high, whether the platform (x64/86) matches, and so on.
Note Select the release version of the DLL (bebug version of C # can also be referenced, but not in C + +).
1.C # end Operation
Code
namespace matlabinterface
{
Public Class Dialog
{
public static bool showselectmsg (string< Span lang= "en-us" > MSG, string title)
{
DialogResult r = MessageBox.Show (msg, title, Messageboxbuttons.yesno, messageboxicon.question);
return r = = Dialogresult.yes;
}
public string Msg { get; set;}
public void showmsg()
{
MessageBox.Show (MSG, "hint ", MessageBoxButtons.OK, messageboxicon.information);
}
}
}
2.matlab Operation
% call C # DLL
% reference absolute path
net. addassembly ( ' R:\Users\GrassPhy\Desktop\MatlabCsharpDemo\MatlabInterface.dll '
% static method call
select = Matlabinterface.dialog. showselectmsg ( "Please select ... ' hint ' if select
disp ( ' yes ' else
disp ( ' no ' end
% member Method invocation
Dialog = matlabinterface. Dialog ();
Dialog. MSG = ' hint information ';
Dialog. showmsg ();
Reference:
using Matlab in C #
C # Matlab calls each other