Download presentation
Presentation is loading. Please wait.
Published byVictoria Patrick Modified over 9 years ago
1
© 2004 The MathWorks, Inc. 1 MATLAB for C/C++ Programmers Support your C/C++ development using MATLAB’s prebuilt graphics functions and trusted numerics
2
© 2004 The MathWorks, Inc. 2 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
3
© 2004 The MathWorks, Inc. 3 Example C/C++ Application
4
© 2004 The MathWorks, Inc. 4 Example C/C++ Application Wireless Networking Simulation w3.antd.nist.gov/wctg/b luetooth/btint.html
5
© 2004 The MathWorks, Inc. 5 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
© 2004 The MathWorks, Inc. 6 Problems Can’t easily visualize your data as plots, especially useful when developing and debugging Difficult to verify your algorithm against a standard
7
© 2004 The MathWorks, Inc. 7 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
8
© 2004 The MathWorks, Inc. 8 Sending Data to MATLAB for Plotting and Testing
9
© 2004 The MathWorks, Inc. 9 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
© 2004 The MathWorks, Inc. 10 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
11
© 2004 The MathWorks, Inc. 11 Data Analysis and Visualization Data from C/C++ application now exits in MATLAB workspace View in array editor Plot with 100s functions Command line of interactive Statistics and fitting Not shown, Demo instead
12
© 2004 The MathWorks, Inc. 12 Sending Commands to MATLAB Simple plot example engEvalString(ep, “stem(data);"); Animation example Complex data structure Transfer to MATLAB data structure Not shown, Demo instead
13
© 2004 The MathWorks, Inc. 13 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)");
14
© 2004 The MathWorks, Inc. 14 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;
15
© 2004 The MathWorks, Inc. 15 Benefit of Sending Data to MATLAB Easily visualize and understand your data Verify your algorithm is correct, reducing risk of failure later
16
© 2004 The MathWorks, Inc. 16 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
© 2004 The MathWorks, Inc. 17 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
18
© 2004 The MathWorks, Inc. 18 Adding MATLAB Modules to Your C/C++ Application
19
© 2004 The MathWorks, Inc. 19 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
© 2004 The MathWorks, Inc. 20 Calling from C S/W Demonstration after
21
© 2004 The MathWorks, Inc. 21 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
22
© 2004 The MathWorks, Inc. 22 Complete Application Development in MATLAB
23
© 2004 The MathWorks, Inc. 23 MATLAB Application Development Build graphical user interface Call C/C++ code The MATLAB Compiler Standalone option Benefits Faster development S/W Demonstration after
24
© 2004 The MathWorks, Inc. 24 Summary
25
© 2004 The MathWorks, Inc. 25 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.