C # and matlab mixed programming - direct call

Add Reference

Call first to use direct solution vs adds the reference to the following references:

And then add the program

using MLApp;

Call matlab

There are four ways to call matlab :( Reference: http://blog.sina.com.cn/s/blog_6317acb50100v0jw.html ):

The first:

MLApp.MLAppClass matlab = new MLApp.MLAppClass(); 

matlab.Visible = 1; 

The second:

MLApp.DIMLApp matlab = null; 

Type matlabAppType = System.Type.GetTypeFromProgID("Matlab.Application"); 

matlab = System.Activator.CreateInstance(matlabAppType) as MLApp.DIMLApp; 

matlab.Visible = 1; 

Third:

MLApp.MLApp matlab = null; 

Type matlabAppType = System.Type.GetTypeFromProgID("Matlab.Application"); 

matlab = System.Activator.CreateInstance(matlabAppType) as MLApp.MLApp; 

matlab.Visible = 1; 

Fourth:

MLApp.MLApp matlab = new MLApp.MLApp(); 

matlab.Visible = 1; 

Use matlab command

After calling matlab can be used directly: matlab.Execute (the Command String); run matlab command.

Note that at this time is in the program directory project debug directory, so if you are using the matlab .m files you need to copy the file to the directory, or use the following method:

string path_project = Directory.GetCurrentDirectory();//工程文件的路径如bin下面的debug  

string path_matlab = "cd('" + path_project + "')";//自定义matlab工作路径,这里我注释调用   

matlab.Execute(path_matlab);

The program directory to the specified directory.

 

Call the following example:

First, call matlab program

command = @"matlabDrawLine()";

matlab.Execute(command);

Second, run directly use string editing commands:

command = "t=2:0.2:4*pi;y=sin(t);plot(t,y)";

matlab.Execute(command);

 

Note that the last plus matlab.Quit ();

 

Also note matlab variables in the form of a matrix organization, you may need to reference as needed:

 

using MathWorks.MATLAB.NET.Arrays;

using MathWorks.MATLAB.NET.Utility;

Published 19 original articles · won praise 2 · Views 5172

Guess you like

Origin blog.csdn.net/gunjiu4462/article/details/88359090