Extract multiple parameters from matlab in c #

Source: Internet
Author: User

I tried to help others complete their work, but I still don't know the C # language. Well, after a long time, I finally finished it for him. Writing a blog today is not just about this, but about it with caution. For example, how to extract multiple parameters from matlab in c.

C # I have never learned, but I have learned C ++. Fortunately, I am not illiterate. Matlab is also used, but not very familiar with it. What we encountered today is how to call matlab in C.

The first step is to configure and learn some experience from others.

Friend Computer Configuration list:

1. Microsoft Visual Studio 2010

(2) Matlab R2012a

(3) window7


1. installation:

First install Matlab;

Install Visual Studio;

Install mcrinstall.exe. After installing Matlab, find C: "Program Files \ MATLAB \ R2012a \ toolbox \ compiler \ deploy \ win32 here.

Configuration path:

Click my computer-properties-advanced-Environment Variable-system variable-PATH-edit. Do not delete the previous string in the variable value input box, add the MCR installation path at the beginning, for example, C: "Program Files \ MATLAB Compiler Runtime \ v80 \ bin \ win32;

Confirm, save, and restart your computer.

2. Input mbuild-setup in the Command window of matlab to display the following:


> Mbuild-setup
Please choose your compiler for building standalone MATLAB applications: wocould you like mbuild to locate installed compilers [y]/n?
N % select n

Select a compiler:
Lcc-win32 C 2.4.1
[2] Microsoft Visual C + + 6.0
[3] Microsoft Visual C ++. NET 2003
[4] Microsoft Visual C + + 2005
[5] Microsoft Visual C ++ 2005 Express Edition
[6] Microsoft Visual C + + 2008
[7] Microsoft Visual C + + 2010
[0] None Compiler:
4% select 4. other compilers can select the appropriate option. I have not verified it.

The default location for Microsoft Visual C ++ 2010 compilers is C: \ Program Files \ Microsoft Visual Studio 10, but that directory does not exist on this machine. use C: \ Program Files \ Microsoft Visual Studio 10.0 anyway [y]/n?
Y % select y, because ta's computer installation is compared, all are default paths

Please verify your choices: Compiler: Microsoft Visual C ++ 20010 Location: C: \ Program Files \ Microsoft Visual Studio 10.0 Are these correct [y]/n? Y %. Check the preceding information. If you select y
Warning: MBUILD requires that the Microsoft Visual C ++ 10.0 directories "VC" and "Common7" be located within the same parent directory. MBUILD setup expected to find directories named "Common7" and "VC" in the directory: "C: \ Program Files \ Microsoft Visual Studio 10 ". trying to update options file: C: \ Documents and Settings \ Administrator \ Application Data \ MathWorks \ MATLAB \ R2010a \ compopts. bat From tem Plate: C: \ PROGRA ~ 1 \ MATLAB \ R2010a \ bin \ win32 \ mbuildopts \ msvc80compp. bat
Done... the matlab compiler has been set up successfully. 3. Compile m files:


Function d = analyze (filePath, rate, pace)
% D (1): walk_rateX, Step Frequency
% D (2): step_lengthX: Step Size
% D (3): step_lengthZ: stride

% FilePath: Specifies the file storage path. Use the absolute path, which is of the string type.
% Rate: Sampling rate, int type
% Pace: walking speed, double or float type

% Clear all;
A = load (filePath );
B = ';

X = B (3 ,:);
% Y = B (4 ,:);
Z = B (5 ,:);

% X = B (12, :)-10;
% Y = B (13 ,:);
% Z = B (14 ,:);


Figure
Plot (x, 'R ');
Grid on;
Title ('x axis acceleration ');

% Figure
% Plot (y, 'G ');
% Grid on;
% Title ('y axis acceleration ');

Figure
Plot (z, 'B ');
Grid on;
Title ('z axis acceleration ');

[ResultX, lagsX] = xcov (x, 'unbiased ');
% [ResultY, lagsY] = xcov (y, 'unbiased ');
[ResultZ, lagsZ] = xcov (z, 'unbiased ');

Y = resultX;
S = kalman (Y );
ResultX = s;
ScatterDataX = show (resultX, 'auto-correlation coefficient of acceleration in the x axis ');

[Pai_ratex, step_lengthX] = gaitPara (scatterDataX, rate, pace );

D = [];
D (1) = pai_ratex;
D (2) = step_lengthX;

% Figure
% Plot (resultY, 'G ');
% Grid on;
% Title ('auto-correlation coefficient of acceleration in y axis ');

Y = resultZ;
S = kalman (Y );
ResultZ = s;
ScatterDataZ = show (resultZ, 'auto-correlation coefficient of acceleration in the z axis ');
[Pai_ratez, step_lengthZ] = gaitPara (scatterDataZ, rate, pace );
D (3) = step_lengthZ;

The end code is ugly. There are several other files that have not been pasted out.

4. Establish a matlab Project

In matlab, click "File-new-Development Project" and select the Project SAVE directory and Project name, such as E: "And magicpro. prj type selection. NET Component. NET). If you want to generate a more general COM Component, select Generic COM Component. Add the m file to the new project. Click the "Build the project" button (the icon of this button is the same as the Build icon of Microsoft development tool) or right-click and select "build". Wait for 3 or 4 minutes. Created successfully.

5. Reference in C #, mainly the button4_Click () method.


Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using MathWorks. MATLAB. NET. Arrays; // you need to find the corresponding dll under the tool you just installed, and then introduce
Using butaipro; // you need to find the corresponding dll file in the matlab project just created.

Namespace test
{
Public partial class Form2: Form
{
PersonDB personDB = null;
Public Form2 ()
{
InitializeComponent ();
PersonDB = new PersonDB ();

}


Private void button#click (object sender, EventArgs e)
{
FrmDesign frm = new frmDesign ();
This. Hide ();
Frm. Show ();

}


Private void button2_Click (object sender, EventArgs e)
{
Application. Exit ();
}

Private void button3_Click (object sender, EventArgs e)
{
Person person = personDB. getPersonInfo (Class1.id );
If (null! = Person)
MessageBox. Show ("the info is: \ n" + person. toStirng ());
Else
MessageBox. Show ("No such person found ");

// Form3 frm = new Form3 ();
// Frm. Text = TextBox + personDB. Select ();


// FrmMain frm = new frmMain ();
// This. Hide ();
// Frm. Show ();
}

Private void button4_Click (object sender, EventArgs e)
{
Butaipro. analysis m = new analysis ();
MWArray [] argsout = new MWArray [1];
MWArray [] argsin = new MWArray [] {"C:/data/1.txt", 50, 1.2 };
M. analyze (3, ref argsout, argsin );

MWNumericArray rate = argsout [0] as MWNumericArray;
MWNumericArray race_length = argsout [1] as MWNumericArray;
MWNumericArray race_range = argsout [2] as MWNumericArray;

String s_rate = rate. ToString ();
String s_race_length = race_length.ToString ();
String s_race_range = race_range.ToString ();

MessageBox. Show ("Stride =" + s_rate + "\ n step =" + s_race_length + "\ n stride =" + s_race_range );
}


}
}

This is even a usable process. However, when matlab returns multiple functions, the button4_Click () method should be careful.

6. Extract multiple parameters from matlab in c #

From the matlab code I provided above, we can see that an array is returned. After an hour of madness, the method in button4_Click () can get the content in this array. But what if multiple matrices are returned? The following is the experience of a "brother" in Du Niang:

? // Enter the two input parameters that you want to input here. To support the good general purpose of the matrix, you have to make it an Array
Double [] a = {1, 2, 3, 4, 5, 6}; // input parameter 1
Double [] B = {2, 4, 6, 8, 10, 12}; // input parameter 2
Double [,] c = new double [3, 2]; // output parameter 1
Double [,] d = new double [3, 2]; // output parameter 2
// All these parameters are Matrices
MWNumericArray ma = new MWNumericArray (3, 2, a); // convert to the format required by matlab
MWNumericArray mb = new MWNumericArray (3, 2, B );
// The output parameter is an MWArray array.
MWArray [] agrsOut = new MWArray [2]; // two output parameters, which must be written
// Output several output parameters can be of different types. For example, the first element is a matrix and the second element is a numerical value.
// Similarly, the input parameter is also an MWArray Array
MWArray [] agrsIn = new MWArray [] {ma, mb };
// Call the function. The ref keyword must be added to the output parameter.
MyFun. MatrixOpera (2, ref agrsOut, agrsIn); // 2 indicates the number of input parameters. The output structure is in argsOut, which is similar to the pointer parameter input of c.
// Convert to get the actual output parameters
MWNumericArray x1 = agrsOut [0] as MWNumericArray;
MWNumericArray x2 = agrsOut [1] as MWNumericArray;
C = (double [,]) x1.ToArray ();
D = (double [,]) x2.ToArray ();
// Note the final conversion of c and d. The conversion of different types varies greatly.
// ToArray () corresponds to the n * m Array
// ToScalarDouble () corresponds to a single value
// ToVetor () corresponds to a 1D Array
After suffering, I found that some of his comments are fatal misunderstandings. The most important thing is:

MyFun. MatrixOpera (2, ref agrsOut, agrsIn); // 2 indicates the number of input parameters. The output structure is in argsOut, which is similar to the pointer parameter input of c.

This 2 is not the number of input parameters, it is the number of output parameters, which indicates that I output two MWNumericArray ....

Of course, the next step will be smooth. How should we do it ....

 

In a rush to write it down, I always feel like I am a CV warrior, but there is no way, I know too little, I can only rely on the power of a large number of network giants, I hope I can improve it in the future .......

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.