addSignal
Description
addSignal(
adds signals specified by the block output port handles to the specified model code
mappings.coderMapObj
,portHandle
)
This function does not apply to signals that originate from root-level Inport blocks.
addSignal(
specifies one or more name-value arguments in addition to the input argument in the previous
syntax.coderMapObj
,portHandle
,Name=Value
)
Examples
Programmatically Add and Remove Signal Lines in Code Mappings of Simulink Models
Use the programmatic interface to add and remove signals in the code mapping configuration of a Simulink model.
To interactively observe how your commands are reflected in the Code Mappings editor, make sure it is open with the Signals/States tab selected. To learn how to open the Code Mappings editor, see Open the Code Mappings Editor – C.
Open the model CoderMapAPI
.
simulinkModel = "CoderMapAPI";
open_system(simulinkModel);
Use the function coder.mapping.api.get
to retrieve the code mapping object of the model.
codeMapObj = coder.mapping.api.get(simulinkModel);
Get the handles of the output ports of the four Gain
blocks and store them in variables.
port_handle_1 = get_param(simulinkModel+"/gain_1","PortHandles").Outport; port_handle_2 = get_param(simulinkModel+"/gain_2","PortHandles").Outport; port_handle_3 = get_param(simulinkModel+"/gain_3","PortHandles").Outport; port_handle_4 = get_param(simulinkModel+"/gain_4","PortHandles").Outport
port_handle_4 = 302.0016
Use the function addSignal
to add the signal to the code mappings of the model. Set the storage class of sig_1
and sig_3
to ImportedExtern
and the storage class of sig_2
and sig_4
to ImportedExternPointer
. Update the model.
addSignal(codeMapObj,[port_handle_1,port_handle_3],StorageClass="ImportedExtern") addSignal(codeMapObj,[port_handle_2,port_handle_4],StorageClass="ImportedExternPointer") set_param(simulinkModel,"SimulationCommand","update")
Use the slbuild
command to generate code from the model.
evalc("slbuild(simulinkModel)");
Signals with ImportedExtern
and ImportedExternPointer
storage classes are declared in separate sections of the generated private header file of the model. Store the name of the private header file in the variable priv_h_file
.
priv_h_file = fullfile(simulinkModel+"_grt_rtw",simulinkModel+"_private.h")
priv_h_file = "CoderMapAPI_grt_rtw/CoderMapAPI_private.h"
The declarations of the signals appear as the following code in the header file:
/* Imported (extern) block signals */ extern real_T sig__1; /* '<Root>/gain_1' */ extern real_T sig__3; /* '<Root>/gain_3' */ extern real_T in_port_1; /* '<Root>/in_port_1' */ extern real_T in_port_2; /* '<Root>/in_port_2' */ /* Imported (extern) pointer block signals */ extern real_T *sig__2; /* '<Root>/gain_2' */ extern real_T *sig__4; /* '<Root>/gain_4' */ extern real_T *in_port_3; /* '<Root>/in_port_3' */ extern real_T *in_port_4; /* '<Root>/in_port_4' */
The signals are defined according to the specified storage classes.
To open the header file, enter this command in the MATLAB Command Window.
edit(priv_h_file)
Use the function removeSignal
to remove the first two signal lines from the code mappings of the model. Update the model.
removeSignal(codeMapObj,[port_handle_1,port_handle_2]) set_param(simulinkModel,"SimulationCommand","update")
Rebuild the model with the slbuild
command.
evalc("slbuild(simulinkModel)");
The updated sections appear as the following code in the header file.
/* Imported (extern) block signals */ extern real_T sig__3; /* '<Root>/gain_3' */ extern real_T in_port_1; /* '<Root>/in_port_1' */ extern real_T in_port_2; /* '<Root>/in_port_2' */ /* Imported (extern) pointer block signals */ extern real_T *sig__4; /* '<Root>/gain_4' */ extern real_T *in_port_3; /* '<Root>/in_port_3' */ extern real_T *in_port_4; /* '<Root>/in_port_4' */
The removed signals no longer appear in the header file.
Input Arguments
coderMapObj
— Code mapping object
CodeMapping
object
Code mapping object (model code mappings) returned by a call to function
coder.mapping.api.get
.
portHandle
— Signal to add to code mappings
block output port handle | port handle array
Signal to add to the code mappings, specified as the output port handle of the source block of the signal. To add multiple signals simultaneously, specify an output port handle array.
Example: addSignal(coderMapObj,[outport_1_Handle,outport_2_Handle])
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: StorageClass="ExportedGlobal"
StorageClass
— Name of storage class
"Auto"
| "ExportedGlobal"
| ...
The name of the storage class to set for the specified signals, specified as one of these values:
"Auto"
"ExportedGlobal"
"ImportedExtern"
"ImportedExternPointer"
To learn more about signal data configuration for code generation, see Configure Signal Data for C Code Generation.
Identifier
— Name of variable
character vector | string scalar
Name for the variable that represents the signal data in the generated code.
Data Types: char
| string
Version History
Introduced in R2020b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
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
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)