Main Content

Hello World

This example shows how to 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 accelerate your MATLAB algorithm. You can run the MEX function to check for run-time errors.

Prerequisites

There are no prerequisites for this example.

About the 'hello_world' Function

The hello_world.m function simply returns the string 'Hello World!'.

type hello_world
function y = hello_world 
%#codegen
y = 'Hello World!';

The %#codegen directive indicates that the MATLAB code is intended for code generation.

Generate the MEX Function

First, generate a MEX function using the command codegen followed by the name of the MATLAB file to compile.

codegen hello_world

By default, codegen generates a MEX function named hello_world_mex in the current folder. This allows you to test the MATLAB code and MEX function and compare the results.

Run the MEX Function

Run the MEX function to compare its behavior to that of the original MATLAB function and to check for run-time errors.

hello_world_mex
ans = 
'Hello World!'