MATLAB Examples

Generate a MEX function from a simple MATLAB function using the codegen command. You can use codegen to check that your MATLAB code is suitable for code generation and, in many cases, to

The recommended workflow for generating C code from a MATLAB function using the codegen command. These are the steps:

Test the output of generated code by using MATLAB® unit tests with MATLAB® Coder™.

Test external C code by using MATLAB® unit tests with MATLAB® Coder™.

Pass data by reference to and from an external C function.

Use the ConstantInputs parameter to control whether a MEX function checks the value of a constant input argument.

Configure the build for external C/C++ code from within a MATLAB® function. Configure the build within a function so that you can more easily integrate it with other projects.

Integrate a simple C function with MATLAB® code by using coder.ceval. Consider the MATLAB function, mathOps:

Call a C function that uses data types that are not natively defined within MATLAB®.

Register and use a Microsoft® Visual C/C++ (MSVC) toolchain running on a 64-bit Windows® platform to compile a 32-bit dynamic link library (DLL). This example uses a Microsoft® compiler.

Generate a standalone C library from MATLAB code that implements a simple Sobel filter that performs edge detection on images. The example also shows how to generate and test a MEX function in

Generate a standalone C library from MATLAB code that reads a file from disk using the functions fopen/fread/fclose.

Register and use a toolchain to compile an executable. This example uses Intel® Compiler, but the concepts and API shown below can be used for any toolchain. The registered toolchain can be

Generate a MEX function and C source code from MATLAB code that performs portfolio optimization using the Black Litterman approach.

Generate C code for a MATLAB Kalman filter function, kalmanfilter , which estimates the position of a moving object based on past noisy measurements. It also shows how to generate a MEX

Integrate external code that operates on a C style array with MATLAB® code. The external code computes a summation over array data. You can customize the code to change the input data or

Control whether structure arguments to generated entry-point functions are passed by reference or by value.

Generate code for a function that uses enumerated types. In this example, the enumerated types inherit from base type int32. The base type can be int8, uint8, int16, uint16, or int32.

Name the C structure type to use in code generated for a global structure.

Write a MATLAB function that uses structure arrays so that it is suitable for code generation. For code generation, you must first create a scalar template version of the structure before

Use codegen to generate code for an image classification application that uses deep learning on Intel® processors. The generated code takes advantage of the Intel Math Kernel Library for

Use cnncodegen to generate code for a Logo classification application that uses deep learning on ARM® processors. The logo classification application uses the LogoNet series network to

Generate code for a MATLAB algorithm that runs a simulation of bouncing "atoms" and returns the result after a number of iterations. There are no upper bounds on the number of atoms that the

Use generated code to accelerate an application that you deploy with MATLAB® Compiler. The example accelerates an algorithm by using MATLAB® Coder™ to generate a MEX version of the

Accelerate MATLAB algorithm execution using a generated MEX function. It uses the codegen command to generate a MEX function for a complicated application that uses multiple MATLAB files.

These examples are using Einstein's General Relativity to calculate geodesics in curved space-time.

Accelerate the execution of a MATLAB algorithm that uses MATLAB classes. The classes create a reverberation effect, that is, the "echo" you hear in a large empty room.

Generate a MEX function from a MATLAB function, compute_average , that uses persistent variables. It illustrates that you must clear the state of persistent variables before using the

Generate a standalone C library from MATLAB code that applies a simple histogram equalization function to images to improve image contrast. The example uses parfor to process each of the

This example shows how to generate code from packData and unpackData

Pack and unpack data using the provided packData and unpackData functions

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office