MatLab API in C++ Christopher Dabney. Purpose MatLab … MatLab … is an interpreted scripting language is an interpreted scripting language conversion to.

Slides:



Advertisements
Similar presentations
The Complete Technical Analysis and Development Environment An attractive alternative to MATLAB and GAUSS - Physics World.
Advertisements

R Packages Davor Cubranic SCARL, Dept. of Statistics.
Software & Services Group, Developer Products Division Copyright© 2010, Intel Corporation. All rights reserved. *Other brands and names are the property.
MATLAB MATLAB is a high-level technical computing language and
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
C Module System C and Data Structures Baojian Hua
Tuesday, January 25, Management of Information Systems: Mini-3 Spring 2000.
1 CS6320 – Why Servlets? L. Grewe 2 What is a Servlet? Servlets are Java programs that can be run dynamically from a Web Server Servlets are Java programs.
Chapter 10 Application Development. Chapter Goals Describe the application development process and the role of methodologies, models and tools Compare.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Copyright © 2006 by The McGraw-Hill Companies,
Road Runner C# -> C++ ->Delphi Code Conversion Project Totte Karlsson.
Programming In C++ Spring Semester 2013 Programming In C++, Lecture 1.
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
© 2004 The MathWorks, Inc. 1 MATLAB for C/C++ Programmers Support your C/C++ development using MATLAB’s prebuilt graphics functions and trusted numerics.
Dr. Chris Musselle – Consultant R Meets Julia Dr Chris Musselle.
Joshua Alexander University of Oklahoma – IT/OSCER ACI-REF Virtual Residency Workshop Monday June 1, 2015 Deploying Community Codes.
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Programming Languages Machine.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
CHAPTER FOUR COMPUTER SOFTWARE.
Java Introduction to JNI Prepared by Humaira Siddiqui.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Running ORGANON in R Peter Gould and David Marshall Growth Model User Group December 9, 2011.
Eng Ship Structures 1 Introduction to Matlab.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Navigation and Ancillary Information Facility NIF Preparing for Programming Using the SPICE Toolkit March 2010.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
Programming with Visual Studio 2005.NET A short review of the process.
Programming with Visual Studio.NET A short review of the process.
Microsoft Visual Basic 2005 BASICS Lesson 1 A First Look at Microsoft Visual Basic.
PROGRAMMING LANGUAGES FOR WHEN USING QUANT ANALYSIS FOR HIGH FREQUENCY TRADING.
Development of a Distributed MATLAB Environment with Real-Time Data Visualization Authors: Joseph Diamond, Richard McEver Affiliation: Dr. Jian Huang,
Computing System Fundamentals 3.1 Language Translators.
Siena Computational Crystallography School 2005
EGR 115 Introduction to Computing for Engineers Introduction to Computer Programming Wednesday 27 Aug 2014 EGR 115 Introduction to Computing for Engineers.
1 How to Install OpenGL u Software running under Microsoft Windows makes extensive use of "dynamic link libraries." A dynamic link library (DLL) is a set.
CIS 601 Fall 2003 Introduction to MATLAB Longin Jan Latecki Based on the lectures of Rolf Lakaemper and David Young.
Open project in Microsoft Visual Studio → build program in “Release” mode.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Scripting Languages Info derived largely from Programming Language Pragmatics, by Michael Scott.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
1 Visual Studio 2005 Options for Debug Mode: C++, Fortran, Linker December 8, 2009 Intel Compiler Version
Introduction to UML and Rational Rose UML - Unified Modeling Language Rational Rose 98 - a GUI tool to systematically develop software through the following.
1 Programming and problem solving in C, Maxima, and Excel.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
Visual Linker ADD presentation. slide 2  Project Overview And Vision  Project System View  The System Life Cycle  Data Model and.
Python Scripting for Computational Science CPS 5401 Fall 2014 Shirley Moore, Instructor October 6,
Programming vs. Packaged
Chapter 2: The Visual Studio .NET Development Environment
OpenGL project setup.
Computer Application in Engineering Design
Introduction to Computer CC111
Software for scientific calculations
MATLAB Basics Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
CO6025 Advanced Programming
Chapter 2 Setup.
CMPE 152: Compiler Design ANTLR 4 and C++
Programming vs. Packaged
Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
Makefiles and Notes on Programming Assignment PA2
Visual Studio 2005 Options for Release Mode: C++, Fortran, Linker
Simulation And Modeling
C++/Java/COM Interoperability
ENERGY 211 / CME 211 Lecture 28 December 1, 2008.
Graphing Using Processing
SPL – PS1 Introduction to C++.
Presentation transcript:

MatLab API in C++ Christopher Dabney

Purpose MatLab … MatLab … is an interpreted scripting language is an interpreted scripting language conversion to object code is at runtime; computation time can be a little slow conversion to object code is at runtime; computation time can be a little slow has excellent prototyping and plotting functionality has excellent prototyping and plotting functionality contains convenient and very robust matrix operation packages contains convenient and very robust matrix operation packages

Purpose (cont) C++ C++ is a programming language, optimal and with high speed floating point computation is a programming language, optimal and with high speed floating point computation is non-trivial to produce visual effects in is non-trivial to produce visual effects in eg. plotting, GUIs, nice tables eg. plotting, GUIs, nice tables can be difficult to secure a robust vector algebra package for can be difficult to secure a robust vector algebra package for

Purpose (cont) Solution: C++ invoking MatLab commands Solution: C++ invoking MatLab commands number crunching in C++ number crunching in C++ matrix operations in MatLab matrix operations in MatLab plotting, graphing, tables in MatLab plotting, graphing, tables in MatLab For programmers with a robust & complex C++ program intending to plot results they are already obtaining For programmers with a robust & complex C++ program intending to plot results they are already obtaining For MatLab programming with scripts which frequently lag For MatLab programming with scripts which frequently lag

Interacting with MatLab Not restricted to C++; Java, Perl, Fortran, and other languages can do it also Not restricted to C++; Java, Perl, Fortran, and other languages can do it also Not restricted to the MS Visual Studio environment the demos are written in Not restricted to the MS Visual Studio environment the demos are written in Also possible to invoke a C++ routine from MatLab’s environment Also possible to invoke a C++ routine from MatLab’s environment Three ways to interact Three ways to interact Send Data from C++ to MatLab Send Data from C++ to MatLab Call a MatLab function from C++ Call a MatLab function from C++ Generate a Dynamic Link Library (dll) file from a.m file Generate a Dynamic Link Library (dll) file from a.m file We will use the first one We will use the first one

Demo Requirements A C++ Compiler A C++ Compiler the demo uses MS Visual C++ v.6.0 the demo uses MS Visual C++ v.6.0 MatLab MatLab the demo uses MatLab v the demo uses MatLab v Assume’s prior programming experience in both MatLab and C++ Assume’s prior programming experience in both MatLab and C++ no explanations are given for the commands no explanations are given for the commands Following slides explain setting up the demonstration program environment Following slides explain setting up the demonstration program environment

Setting up the Environment Creating paths to MatLab: Creating paths to MatLab: - Tools -> Options - Tools -> Options - Directories Tab - Directories Tab - Select "Include files" from the drop-down list - Select "Include files" from the drop-down list - Add "C:\MATLAB701\EXTERN\INCLUDE" to the list - Add "C:\MATLAB701\EXTERN\INCLUDE" to the list - Select "Library Files" from the drop-down list - Select "Library Files" from the drop-down list - Add ”C:\MATLAB701\EXTERN\LIB\WIN32\MICROSOFT\MSVC60“ - Add ”C:\MATLAB701\EXTERN\LIB\WIN32\MICROSOFT\MSVC60“ Library paths vary depending on the language and compiler Library paths vary depending on the language and compiler

Screenshot

Setting up the Environment (cont) Two ways to include the appropriate library files: Two ways to include the appropriate library files: Method 1) - Project -> Settings Method 1) - Project -> Settings - Links Tab - Links Tab Under "Object/Library Module:", add three file names: Under "Object/Library Module:", add three file names: - Libmx.lib - libmex.lib - libeng.lib Method 2) Add the following lines of code just under the Method 2) Add the following lines of code just under the includes in the source code: includes in the source code: #pragma comment( lib, "Libmx.lib" ) #pragma comment( lib, "Libmx.lib" ) #pragma comment( lib, "libmex.lib" ) #pragma comment( lib, "libmex.lib" ) #pragma comment( lib, "libeng.lib" ) #pragma comment( lib, "libeng.lib" )

Screenshot

Connecting to MatLab Engine Header: #include, MatLab’s Engine Header: #include, MatLab’s Engine In the program, create a pointer to it: In the program, create a pointer to it: Engine *m_pEngine; m_pEngine = engOpen(NULL); if (m_pEngine == NULL) { cout << "Error: Not Found” << endl; << endl; exit(1); exit(1);}

Invoking Commands engEvalString(Engine* ptr, string cmd); engEvalString(Engine* ptr, string cmd); engEvalString(m_pEngine, “x = 1:1:10);“ ); engEvalString(m_pEngine, “y = x.^2;“ ); engEvalString(m_pEngine, “plot(x,y);“ ); Enables programmer to invoke any command in MatLab from the C++ platform (convenient) Enables programmer to invoke any command in MatLab from the C++ platform (convenient) Not special by itself - might as well work straight from MatLab Not special by itself - might as well work straight from MatLab

IO between C++ and MatLab Use the “matrix array” mxArray data type Use the “matrix array” mxArray data type Every variable in MatLab is a matrix – for C++ to exchange data with it, a data type that both C++ and MatLab recognize is needed Every variable in MatLab is a matrix – for C++ to exchange data with it, a data type that both C++ and MatLab recognize is needed mxArray can “bundle” C++ variables as matrices, so both platforms recognize this data type mxArray can “bundle” C++ variables as matrices, so both platforms recognize this data type

Inserting Values (input) To pass a variable, eg. x[0], into MatLab, create an mxArray for it To pass a variable, eg. x[0], into MatLab, create an mxArray for it - Allocate the space (mxCreateDoubleMatrix) - Copy the value (memcpy) - Name the variable in the Engine (engPutVariable) double x[0]; mxArray *m_X; m_X=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(m_X), (void *)x, sizeof(double)*1); engPutVariable(m_pEngine, "x", m_X); Pointer m_pEngine is used to specify the engine Pointer m_pEngine is used to specify the engine Variable x in the MatLab Engine gets the value of x[0] in C++ Variable x in the MatLab Engine gets the value of x[0] in C++

Extracting Values (output) To extract the MatLab Engine’s variable z, pull it into C++ as a mxArray matrix, then extract the bundled value out of that mxArray and into a C++ variable To extract the MatLab Engine’s variable z, pull it into C++ as a mxArray matrix, then extract the bundled value out of that mxArray and into a C++ variable double *cresult; mxArray *mresult; mresult = engGetVariable(m_pEngine,"z"); cresult = mxGetPr(mresult); cout << cresult[0]; mxGetPr returns a pointer to a copy of the double value z stored in the MatLab Engine mxGetPr returns a pointer to a copy of the double value z stored in the MatLab Engine Warning: Unpredictable fatal errors occur if the data type in MatLab doesn’t closely resemble the data type in C++ eg. Copying a 3x1 vector into a scalar Warning: Unpredictable fatal errors occur if the data type in MatLab doesn’t closely resemble the data type in C++ eg. Copying a 3x1 vector into a scalar

Passing Arrays & Matrices To pass arrays (vector), adjust the size parameters of the memory allocation and copy routine to match the dimensions of the array being passed To pass arrays (vector), adjust the size parameters of the memory allocation and copy routine to match the dimensions of the array being passed Pass a vector a of dimensions: 1 x SIZE Pass a vector a of dimensions: 1 x SIZE double a[SIZE]; mxArray *A; // assume a gets initialized, all values A=mxCreateDoubleMatrix(1, SIZE, mxREAL); memcpy((void *)mxGetPr(A), (void *)a, sizeof(double)*SIZE); engPutVariable(m_pEngine, "a", A); Copies entire C++ array a into the MatLab Engine’s a Copies entire C++ array a into the MatLab Engine’s a

Passing Arrays & Matrices (cont) Pass a matrix of dimensions: SIZE x SIZE Pass a matrix of dimensions: SIZE x SIZE double c[SIZE][SIZE]; // assume c gets initialized, all of it mxArray *mxc; mxc = mxCreateDoubleMatrix(SIZE, SIZE, mxREAL); memcpy((void *) mxGetPr(mxc), (void *)c, sizeof(double)*SIZE*SIZE); engPutVariable(m_pEngine, "c", mxc); engEvalString(m_pEngine, "c = c';"); Note: C++ has row-major storage, and MatLab has column-major storage. Thus a matrix being exchanged must be transposed, either before or after the exchange, to maintain the matrix’s semantics Note: C++ has row-major storage, and MatLab has column-major storage. Thus a matrix being exchanged must be transposed, either before or after the exchange, to maintain the matrix’s semantics

See the Demos Program Demonstrates all of these features so far in Microsoft Visual Development Studio C++ v.6.0 Demonstrates all of these features so far in Microsoft Visual Development Studio C++ v.6.0 Demonstrates graph plots, invoking commands, input, output, and passing vectors and matrices. Demonstrates graph plots, invoking commands, input, output, and passing vectors and matrices.

Internet References Welcome to Zhenwang's Homepage (Q&A) Welcome to Zhenwang's Homepage (Q&A) A Tutorial to Call MATLAB Functions from Within A C/C++ Program A Tutorial to Call MATLAB Functions from Within A C/C++ Program Microsoft Visual Studio C++ & Interfacing Matlab with C/C++, Java terfacings.doc terfacings.doc MatLab – The Language of Technical Computing, External Interfaces MatLab – The Language of Technical Computing, External Interfaces

Questions? …

Thank You No MatLab matrices were harmed in the making of this presentation.