Proficient in combined VC and Matlab programming <10> -- Comprehensive Example 2
Author: Deng Ke
The tool used in this article: vc6.0 + php6.5
In this chapter, the MATLAB Neural Network Toolbox is called in VC. The program used in this article is part of a Master's thesis. Therefore, the source program is not provided, this example shows how to call COM components in VC to exchange VC and Matlab data. First, two neural network functions are completed in MATLAB, one for test and the other for prediction.
1. test functions:
Function [W1, B1, W2, B2, ET, a2] = sjhsjy (P, T, me, eg, LR, P2, T2, S1) [pn, meanp, stdp, TN, meant, stdt] = prestd (P, T); % preprocessing [W1, B1, W2, B2] = initff (Pn, S1, ''tansig '', TN, ''purelin''); % data initialization df = 10; TP = [DF me eg LR]; [W1, B1, W2, B2, TP, tr] = trainbpx (W1, B1, ''tansig ''); % sample training p2n = trastd (P2, meanp, stdp );...... post-processing % must be non-negative
This function is used to test the error of BP neural networks. P, T, me, eg, LR, P2, T2, and S1 are input parameters. W1, B1, W2, B2, ET, a2 is the output parameter.
2. prediction functions:
Function [a2] = sjhsyc (P, T, P2, me, eg, LR, S1) [pn, meanp, stdp, TN, meant, stdt] = prestd (p, t); [W1, B1, W2, B2] = initff (Pn, S1, ''tansig '', TN, ''purelin '');........ parameter output
P, T, me, eg, LR, P2, T2, S1 are input parameters A2 are output parameters, that is, the expected results
3. In Matlab, the two functions are made into COM components. For details, refer to the joint programming of VC and MATLAB. <3>.
4. Create a dialog box-based VC project and introduce COM components to VC. For details, refer to the joint programming of VC and MATLAB. <3>, after that, we can see two new classes isjhsjy and isjhsyc, as shown in 1 in classview. they implement the functions sjhsjy respectively. m and sjhsyc. M features.
Figure 1
View the isjhsjy-like parameters carefully, as shown in figure 2.
Figure 2
In the isjhsjy class, the call function format is described as follows:
ISJHSJY __RPC_FAR * This, /* [in] */ long nargout, /* [out][in] */ VARIANT __RPC_FAR *w1, /* [out][in] */ VARIANT __RPC_FAR *b1, /* [out][in] */ VARIANT __RPC_FAR *w2, /* [out][in] */ VARIANT __RPC_FAR *b2, /* [out][in] */ VARIANT __RPC_FAR *ET, /* [out][in] */ VARIANT __RPC_FAR *a2, /* [in] */ VARIANT P, /* [in] */ VARIANT T, /* [in] */ VARIANT me, /* [in] */ VARIANT eg, /* [in] */ VARIANT lr, /* [in] */ VARIANT P2, /* [in] */ VARIANT T2, /* [in] */ VARIANT S1);
Nargout indicates the number of output parameters. W1, B1, W2, B2, ET, and A2 indicate variant output parameters. P, T, me, eg, LR, P2, T2, s1 is a variant input parameter;
It can be seen that the variant data can easily establish data exchange between VC and MATLAB. For the variant data type, you can view the msdn help documentation or the VC class library manual.
5. key code:
SAFEARRAYBOUND rgsabound1[2];rgsabound1[0].lLbound=0;rgsabound1[1].lLbound=0;rgsabound1[0].cElements=4; rgsabound1[1].cElements=6;VariantInit(&TTT);TTT.vt=VT_R8|VT_ARRAY;TTT.parray=SafeArrayCreate(VT_R8,2,rgsabound1);TTT.parray->pvData=HH2;
The above code is used to convert the input parameter to the variant type in VC, and create a Variant 4 × 6 two-dimensional array. HH2 is a double one-dimensional array, and TT is an input parameter in the function, in the program, the input and output parameters are basically two-dimensional arrays.
6. Call code:
If (failed (coinitialize (null) // COM Initialization {MessageBox ("error");} isjhsjy * Pclass = NULL; hresult hR = cocreateinstance (clsid_sjhsjy, null, clsctx_all, iid_isjhsjy, (void **) & Pclass); If (succeeded (HR) {long int u = 6; hresult HR1 = Pclass-> sjhsjy (u, & W1, & B1, & W2, & B2, & et, & A2, PP, TTT, me, eg, LR, P2, T2, S1); // function call if (! Succeeded (HR1) {MessageBox ("enter correct data"); return;} // function call failure if (succeeded (HR1) {memcpy (W1, w1.parray-> pvdata, 420 * sizeof (double); // convert variant-type result data to double-type memcpy (W2, w2.parray-> pvdata, 240 * sizeof (double); memcpy (B1, b1.parray-> pvdata, 60 * sizeof (double); memcpy (B2, b2.parray-> pvdata, 4 * sizeof (double); memcpy (ET, et. parray-> pvdata, 4 * sizeof (double); memcpy (A2, a2.parray-> pvdata, 4 * sizeof (double); m_edit7.forma T ("% F/R/n % F/R/N", et [0], et [1], et [2], et [3]) ;}} else {MessageBox ("COM component call failed! "," Prompt ", mb_iconhand); exit (0) ;}// Pclass-> release (); couninitialize (); // end com
7. program running result:
Figure 3
Figure 4
Figure 5
Figure 6
Figure 7
Figure 8
The program interface is written in VC. The algorithm calls the MATLAB Neural Network Toolbox. After packaging, the program can be installed and run on a machine without Matlab installed. The installation file is only 3 MB, although MATLAB also has a neural network GUI, it cannot be separated from the MATLAB environment. By Calling Matlab in VC, we can make full use of VC to establish a friendly interface, MATLAB can be used to avoid writing complex algorithms and greatly improve programming efficiency. QQ: 44760299. At present, you are welcome to discuss it together.