Post on 26-Dec-2015
transcript
© 2
004
The
Ma
thW
orks
, In
c.
1
MATLAB for C/C++ Programmers
Support your C/C++ development using MATLAB’s prebuilt graphics functions and trusted numerics
2
© 2
004
The
Ma
thW
orks
, In
c.
Agenda
Example C/C++ application Sending data to MATLAB for plotting and
testing Adding MATLAB S/W modules into your
application Complete application development in MATLAB
© 2
004
The
Ma
thW
orks
, In
c.
3
Example C/C++ Application
4
© 2
004
The
Ma
thW
orks
, In
c.
Example C/C++ Application
Wireless Networking Simulation
w3.antd.nist.gov/wctg/bluetooth/btint.html
5
© 2
004
The
Ma
thW
orks
, In
c.
C/C++ Code and Development
Code• Files• Classes
Development• Build and run• Debug
Difficult to understand data and operation of algorithms
S/W (C_Application) Demonstration after
6
© 2
004
The
Ma
thW
orks
, In
c.
Problems
Can’t easily visualize your data as plots, especially useful when developing and debugging
Difficult to verify your algorithm against a standard
7
© 2
004
The
Ma
thW
orks
, In
c.
Solution: Send Data to MATLAB
High-level interpreted language and development environment
More than1000 built-in functions
Graphics and charting Linear algebra, statistics,
transforms, filtering Can control from another
application
© 2
004
The
Ma
thW
orks
, In
c.
8
Sending Data to MATLABfor Plotting and Testing
9
© 2
004
The
Ma
thW
orks
, In
c.
Calling MATLAB from C/C++
Start MATLAB• matlab.exe /Automation• Optionally start desktop
In C/C++ code• Use MATLAB Engine library
#include “engine.h”Engine *ep;ep = engOpen(NULL);
engPutVariable(…);engGetVariable(…);engEvalString(…);
10
© 2
004
The
Ma
thW
orks
, In
c.
Transferring Data to MATLAB// Data array in Cdouble data_c[10] = {8,1,2,3,2,5,-1,7,8,3};
// Array to store data ready to transfer to MATLABmxArray *data_ml;
// Data array in MATLAB formatdata_ml = mxCreateDoubleMatrix(1,10,mxREAL);
// Copy C array data into MATLAB array datamemcpy((char *)mxGetPr(data_ml),(char *)data_c,10*sizeof(double));
// Transfer to MATLAB
engPutVariable(ep,"data",data_ml);
S/W Demonstration (Sending_Data) after
13
© 2
004
The
Ma
thW
orks
, In
c.
Test Your C/C++ Algorithms
Reconstruct algorithms in MATLAB Call test program to compare C result with M
resultengPutVariable(ep,“input”,input_ml);engPutVariable(ep,“output”,output_ml);engEvalString(ep,“testmyalg(input,output)");
14
© 2
004
The
Ma
thW
orks
, In
c.
Example Algorithm Comparison C++ Code algorithmBits spread=addChips(diffOut[slice(i,1)]);
BitsIEEE802_11b_Transmitter::addChips(const Bits& input) { Bits spreadOut(input.size()*Ns,false);
for (int i=0;i<input.size();++i){ for(int j=0; j<11; ++j) { spreadOut[i*Ns+4*j]= m_chip[j]^input[i]; } }
return spreadOut;}
M Code algorithmTx_chips=reshape(Barker*Tx_symbols',[],1);Tx_samples(1:Samples_per_chip:end)=Tx_chips;
15
© 2
004
The
Ma
thW
orks
, In
c.
Benefit of Sending Data to MATLAB
Easily visualize and understand your data Verify your algorithm is correct, reducing risk
of failure later
16
© 2
004
The
Ma
thW
orks
, In
c.
Problems
No canned math, statistics, numerics and data analysis functions common in technical computing algorithms
You must write them yourself, which can be time consuming, or use other libraries
17
© 2
004
The
Ma
thW
orks
, In
c.
Solution: MATLAB Module for Engineering or Mathematical Components
For heavy engineering or mathematical components of your application
Create a MATLAB module using the many built-in functions • Vector and matrix based• Application specific functions in toolboxes
Convert to a DLL or COM object with the MATLAB compiler • Distribute freely with your application
© 2
004
The
Ma
thW
orks
, In
c.
18
Adding MATLAB Modules toYour C/C++ Application
19
© 2
004
The
Ma
thW
orks
, In
c.
Build Component Example
Design M code function foo.m
Use MATLAB Compiler• Converts MATLAB
Code to standalone exe or library
• Create Foolib.dll Add include and library
files to project See application note
27671
20
© 2
004
The
Ma
thW
orks
, In
c.
Calling from C
S/W Demonstration after
21
© 2
004
The
Ma
thW
orks
, In
c.
Benefits of Using a MATLAB Module
Leave MATLAB to the heavy math and engineering tasks what it is designed for
No need to spend time re-creating functions in C/C++ yourself that are already available in MATLAB
No need to test MATLAB’s trusted numerics
© 2
004
The
Ma
thW
orks
, In
c.
22
Complete ApplicationDevelopment in MATLAB
23
© 2
004
The
Ma
thW
orks
, In
c.
MATLAB Application Development
Build graphical user interface
Call C/C++ code The MATLAB Compiler• Standalone option
Benefits• Faster development
S/W Demonstration after
© 2
004
The
Ma
thW
orks
, In
c.
24
Summary
25
© 2
004
The
Ma
thW
orks
, In
c.
MATLAB Can Support Your C/C++ Development
Send data to MATLAB for plotting and testing• Easily visualize and understand your data• Verify your algorithm is correct
Add MATLAB S/W modules into your application• Leave MATLAB to the heavy math and engineering• No need to spend time re-creating functions in C/C++
Develop complete applications in MATLAB• Faster development technical computing problems