290 likes | 578 Views
MATLAB for C/C++ Programmers. Support your C/C++ development using MATLAB’s prebuilt graphics functions and trusted numerics. Agenda. Example C/C++ application Sending data to MATLAB for plotting and testing Adding MATLAB S/W modules into your application
E N D
MATLAB for C/C++ Programmers Support your C/C++ development using MATLAB’s prebuilt graphics functions and trusted numerics
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
Example C/C++ Application • Wireless Networking Simulation • w3.antd.nist.gov/wctg/bluetooth/btint.html
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
Problems • Can’t easily visualize your data as plots, especially useful when developing and debugging • Difficult to verify your algorithm against a standard
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
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(…);
Transferring Data to MATLAB // Data array in C double data_c[10] = {8,1,2,3,2,5,-1,7,8,3}; // Array to store data ready to transfer to MATLAB mxArray *data_ml; // Data array in MATLAB format data_ml = mxCreateDoubleMatrix(1,10,mxREAL); // Copy C array data into MATLAB array data memcpy((char *)mxGetPr(data_ml),(char *)data_c,10*sizeof(double)); // Transfer to MATLAB engPutVariable(ep,"data",data_ml); S/W Demonstration (Sending_Data) after
Test Your C/C++ Algorithms • Reconstruct algorithms in MATLAB • Call test program to compare C result with M result engPutVariable(ep,“input”,input_ml); engPutVariable(ep,“output”,output_ml); engEvalString(ep,“testmyalg(input,output)");
Example Algorithm Comparison • C++ Code algorithm Bits spread=addChips(diffOut[slice(i,1)]); Bits IEEE802_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 algorithm Tx_chips=reshape(Barker*Tx_symbols',[],1); Tx_samples(1:Samples_per_chip:end)=Tx_chips;
Benefit of Sending Data to MATLAB • Easily visualize and understand your data • Verify your algorithm is correct, reducing risk of failure later
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
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
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 Build Component Example
Calling from C S/W Demonstration after
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
MATLAB Application Development • Build graphical user interface • Call C/C++ code • The MATLAB Compiler • Standalone option • Benefits • Faster development S/W Demonstration after
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