MATLAB for C/C++ Programmers

23
© 2004 The MathWorks, Inc. 1 MATLAB for C/C++ Programmers Support your C/C++ development using MATLAB’s prebuilt graphics functions and trusted numerics

description

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 - PowerPoint PPT Presentation

Transcript of MATLAB for C/C++ Programmers

Page 1: MATLAB for C/C++ Programmers

© 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

Page 2: MATLAB for C/C++ Programmers

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

Page 3: MATLAB for C/C++ Programmers

© 2

004

The

Ma

thW

orks

, In

c.

3

Example C/C++ Application

Page 4: MATLAB for C/C++ Programmers

4

© 2

004

The

Ma

thW

orks

, In

c.

Example C/C++ Application

Wireless Networking Simulation

w3.antd.nist.gov/wctg/bluetooth/btint.html

Page 5: MATLAB for C/C++ Programmers

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

Page 6: MATLAB for C/C++ Programmers

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

Page 7: MATLAB for C/C++ Programmers

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

Page 8: MATLAB for C/C++ Programmers

© 2

004

The

Ma

thW

orks

, In

c.

8

Sending Data to MATLABfor Plotting and Testing

Page 9: MATLAB for C/C++ Programmers

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(…);

Page 10: MATLAB for C/C++ Programmers

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

Page 11: MATLAB for C/C++ Programmers

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)");

Page 12: MATLAB for C/C++ Programmers

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;

Page 13: MATLAB for C/C++ Programmers

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

Page 14: MATLAB for C/C++ Programmers

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

Page 15: MATLAB for C/C++ Programmers

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

Page 16: MATLAB for C/C++ Programmers

© 2

004

The

Ma

thW

orks

, In

c.

18

Adding MATLAB Modules toYour C/C++ Application

Page 17: MATLAB for C/C++ Programmers

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

Page 18: MATLAB for C/C++ Programmers

20

© 2

004

The

Ma

thW

orks

, In

c.

Calling from C

S/W Demonstration after

Page 19: MATLAB for C/C++ Programmers

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

Page 20: MATLAB for C/C++ Programmers

© 2

004

The

Ma

thW

orks

, In

c.

22

Complete ApplicationDevelopment in MATLAB

Page 21: MATLAB for C/C++ Programmers

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

Page 22: MATLAB for C/C++ Programmers

© 2

004

The

Ma

thW

orks

, In

c.

24

Summary

Page 23: MATLAB for C/C++ Programmers

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