Main Content

coder.target

Determine if code generation target is specified target

Description

example

tf= coder.target(target)returns true (1) if the code generation target istarget. Otherwise, it returns false (0).

If you generate code for MATLAB®classes, MATLAB computes class initial values at class loading time before code generation. If you usecoder.targetin MATLAB class property initialization,coder.target('MATLAB')returns true.

Examples

collapse all

Parametrize a MATLAB function so that it works in MATLAB or in generated code. When the function runs in MATLAB, it calls the MATLAB functionmyabsval. The generated code, however, calls a C library functionmyabsval.

Write a MATLAB functionmyabsval.

functiony = myabsval(u)%#codegeny = abs(u);

Generate a C static library formyabsval, using the-argsoption to specify the size, type, and complexity of the input parameter.

codegen配置:libmyabsval-args{0.0}
Thecodegenfunction creates the library filemyabsval.liband header filemyabsval.hin the folder\codegen\lib\myabsval. (The library file extension can change depending on your platform.) It generates the functionsmyabsval_initializeandmyabsval_terminatein the same folder.

Write a MATLAB function to call the generated C library function usingcoder.ceval.

functiony = callmyabsval(y)%#codegen% Check the target. Do not use coder.ceval if callmyabsval is% executing in MATLABifcoder.target('MATLAB')% Executing in MATLAB, call function myabsvaly = myabsval(y);else% add the required include statements to generated function codecoder.updateBuildInfo('addIncludePaths','$(START_DIR)\codegen\lib\myabsval'); coder.cinclude('myabsval_initialize.h'); coder.cinclude('myabsval.h'); coder.cinclude('myabsval_terminate.h');% Executing in the generated code.% Call the initialize function before calling the% C function for the first timecoder.ceval('myabsval_initialize');% Call the generated C library function myabsvaly = coder.ceval('myabsval',y);% Call the terminate function after% calling the C function for the last timecoder.ceval('myabsval_terminate');end

Generate the MEX functioncallmyabsval_mex. Provide the generated library file at the command line.

codegen配置:mexcallmyabsvalcodegen\lib\myabsval\myabsval.lib-args{-2.75}

Rather than providing the library at the command line, you can usecoder.updateBuildInfoto specify the library within the function. Use this option to preconfigure the build. Add this line to theelseblock:

coder.updateBuildInfo('addLinkObjects','myabsval.lib','$(START_DIR)\codegen\lib\myabsval', 100年,真的,真的);

Note

TheSTART_DIRmacro is only supported for generating code withMATLAB Coder™.

Run the MEX functioncallmyabsval_mexwhich calls the library functionmyabsval.

callmyabsval_mex(-2.75)
ans = 2.7500

Call the MATLAB functioncallmyabsval.

callmyabsval(-2.75)
ans = 2.7500
Thecallmyabsvalfunction exhibits the desired behavior for execution in MATLAB and in code generation.

Input Arguments

collapse all

Code generation target, specified as a character vector or a string scalar. Specify one of these targets.

'MATLAB' Running in MATLAB (not generating code)

'C','C++','CUDA','OpenCL''SystemC','SystemVerilog','Verilog','VHDL'

Supported target languages for code generation

'MEX' Generating a MEX function
'Sfun' Simulating a Simulink®model. Also used for running in Accelerator mode.
'Rtw' Generating a LIB, DLL, or EXE target. Also used for running inSimulink Coderand Rapid Accelerator mode.
'HDL' Generating an HDL target
'Custom' Generating a custom target

Example:tf = coder.target('MATLAB')

Example:tf = coder.target("MATLAB")

Note

In case ofCUDAorSystemCcode generation,coder.target('C++')is alwaystrue.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2011a