Click here to Skip to main content
15,878,970 members
Articles / Programming Languages / C++

Calling Matlab Neural Network From Other EXE's

Rate me:
Please Sign up or sign in to vote.
2.21/5 (13 votes)
12 May 2009CPOL2 min read 89.1K   2K   30   12
This article describes how to make Use of matlab Neural Network Tool Box from Other environments like VB,C++ etc

Introduction

     This Article And Source Code Intended Those Users Who Are Working On Neural Network Related Projects.In This Article ,i am using MATLAB 6.5 Neural Network ToolBox To Create Neural Network And Call The Same From Other Environments Like VB,C++ etc..

Description

           Step 1:-- > Design A Nueral Netwrok Using Matlab NN tool box.

                 for Eg:- net = newlind(P,T);  P = Inputs , T = Targets

[There's Lots Examples In Matlab Which Describes How To Create Neural Network  ]               

            Step 2: --> Write .M file  which create the Nueral Network According to our Inputs.[refer:- CreateNN.M in the demo Project]

            Step 3:--> Add Code To Save Wokspace using the command "save" in the CreateNN.m file   [ you can specify the location ].Now Trained Network Saved.  

           Step  4:--> Write Another .m file which loads Trained Network And Apply Simulate Function.Result can be saved to datbase or a text file.[ refer :--> ApplySimFun.m]

           Step  5:--> Now we have to integrate(execute) .m file as part of our application.In order to that we have to Invoke Matlab As Automation Server.

Demo Project Description

How To Test The Apllication

i wrote two sample file .m files , foldername Samples_NN_Files

step1 , copy Samples_NN_Files  Folder to c:\   
       folder contains two .m file ,
       file ,CreateNN.m  ->  creates the network , save the network to c:\Samples_NN_Files
       file , ApplySimFun.m -> loads the network from c:\Samples_NN_Files apply the    simulate        function, write the result to a log file C:\NNResult.log"

Step2 , u have to call these file(execute) from .exe(vb,vc,c++)   , In order to do that u have to         invoke matlab as automatomation server.  for this purpose i wrote wrapper dll called CMatlabEngine.dll.
       so register the dll ( command line:- regsvr32 CMatlabEngine.dll)  file (Note:  i am using Matlab 6.5 version so , if u are using anyother versions there may be version conflict)
       u can find the dll file in vb_dll_matlab folder ( actually it's vb project for creating         matlab wrapper, so go through the code")          
          
step 3 ,there's sample programm written in vb ,Call_NN_From_VB which call the matlab execute the Network shows the result to testbox

  Conclusion      

          Hope You Will Enjoy.Any Doubts Feel Free To Contact Me.Actually I wrote examples in VB 6.0.But you can Invoke The  CMatlabEngine.dll. from c++ using #import Statement.

Update :- you can watch my screencast on using matlab Neural Network in C# application here 

.This approach doesn't require Matlab to be installed in client machine. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Max Corp.
Singapore Singapore
I am BaijuMax.Now working as Senior Systems Engineer.

Tech. Interests :- C#,Winforms,Asp.net,VB.net,VB,Sqlserver-2000,Analysis services,Matlab,Neural networks etc...

Hobbies :- Watching Sci-Fi Movies.

Dream & Vision:- Intelligent OS , Space Travelling.

Contact:- Baijumax@rediffmail.com,Baijumax@yahoo.com

My Blog:- http://www.baijumax.blogspot.com

Comments and Discussions

 
Questionwater demand forecasting using neural network(matlab) Pin
niranjan258810-Oct-11 1:00
niranjan258810-Oct-11 1:00 
QuestionI have this error Interop type 'MLApp.MLAppClass' cannot be embedded. Use the applicable interface instead. Pin
pocho210120-Sep-11 5:47
pocho210120-Sep-11 5:47 
Generalregarding MATlab Pin
aruntry22-Dec-09 19:13
aruntry22-Dec-09 19:13 
GeneralNeed help Urgently Pin
Member 244656026-Jun-08 7:11
Member 244656026-Jun-08 7:11 
GeneralMATLAB is not installed Pin
Taylor Kobani3-May-08 7:30
Taylor Kobani3-May-08 7:30 
GeneralRe: MATLAB is not installed Pin
BAIJUMAX24-May-08 6:00
professionalBAIJUMAX24-May-08 6:00 
Questionhow to detect abnormal data by training a rule using matlab? Pin
phoebe42116-Mar-08 22:31
phoebe42116-Mar-08 22:31 
Questionplease help me Pin
miro_soft11-Apr-07 12:33
miro_soft11-Apr-07 12:33 
Hi,

i am working with backpropagation ....to make weather predection ....
my input and target are in excel sheet

input is organized like this

365X5 matrix

5 means five days before the day i want to predect...for example i want to predect 1/1/2007 input will be

26/12/2006 27/12/2006 28/12/2006 29/12/2006 31/12/2006

this is my code
p=wk1read('new3');<br />
T=wk1read('target1');<br />
Test=wk1read('new_test');<br />
p=p';<br />
T=T';<br />
Test=Test';<br />
[r,q]=size(p);<br />
s1=5;<br />
[s2,q]=size(T);<br />
[w1,b1]=rands(s1,r);<br />
[w2,b2]=rands(s2,s1);<br />
<br />
<br />
net = newff([min(min(p)) max(max(p))],[5 1],{ 'purelin' 'purelin'},'TRAINRP','LEARNGD','MSE');<br />
net.numInputs=5<br />
net.initFcn='initlay';<br />
net.layers{1}.initFcn='initnw';<br />
net.layers{2}.initFcn='initnw';<br />
%net.inputWeights{1,1}.initFcn='rands';<br />
%net.layerWeights{1,2}.initFcn='rands';<br />
net.biases{1}.initFcn='rands';<br />
net.biases{2}.initFcn='rands';<br />
net = init(net);<br />
<br />
net.trainParam.mc=.95<br />
net.trainParam.lr=.1<br />
net.trainParam.epochs =2000;<br />
net.trainParam.goal = .01;<br />
net = train(net,p,T);<br />
<br />
<br />
Y = sim(net,p);<br />
Y(:,5:10)<br />
T(:,5:10)<br />
plot(p,T,p,Y,'o')<br />
x=sim(net,Test);<br />
x'


the problem is that the training stops before the goal is met becuase of reaching somthing called Minimum gradient

or

maximum number of Epochs is reached

this is the output

TRAINRP, Epoch 0/2000, MSE 1135.09/0.01, Gradient 2148.71/1e-006
TRAINRP, Epoch 25/2000, MSE 9.68716/0.01, Gradient 0.838298/1e-006
TRAINRP, Epoch 50/2000, MSE 9.26178/0.01, Gradient 1.01019/1e-006
TRAINRP, Epoch 75/2000, MSE 9.18916/0.01, Gradient 0.341586/1e-006
TRAINRP, Epoch 100/2000, MSE 9.189/0.01, Gradient 0.00468536/1e-006
TRAINRP, Epoch 125/2000, MSE 9.189/0.01, Gradient 3.40773e-005/1e-006
TRAINRP, Epoch 145/2000, MSE 9.189/0.01, Gradient 7.38052e-007/1e-006
TRAINRP, Minimum gradient reached, performance goal was not met.

as you can see , after certain number of epchos performance goal does not change.....why ??????

please help me

thanks


miro_soft
AnswerRe: please help me Pin
dayananda c s15-Dec-09 23:22
dayananda c s15-Dec-09 23:22 
GeneralMy Big Problem Pin
Anonymous18-Aug-05 21:58
Anonymous18-Aug-05 21:58 
GeneralMy Big Problem-MAHDI FAHMIDEH Pin
Mehdi Fahmideh18-Aug-05 20:53
sussMehdi Fahmideh18-Aug-05 20:53 
GeneralRe: My Big Problem-MAHDI FAHMIDEH Pin
expressdual1-Apr-07 23:37
expressdual1-Apr-07 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.