# Select a web site

## Description

The Trigonometric Function block performs common trigonometric
functions and outputs the result in rad.

### Supported Functions

You can select one of these functions from the Function
parameter list.

FunctionDescriptionMathematical Expression MATLAB Equivalent

Sine of the input

Cosine of the input

Tangent of the input

Inverse sine of the input

Inverse cosine of the input

Inverse tangent of the input

Four-quadrant inverse tangent of the input

Hyperbolic sine of the input

Hyperbolic cosine of the input

Hyperbolic tangent of the input

Inverse hyperbolic sine of the input

Inverse hyperbolic cosine of the input

Inverse hyperbolic tangent of the input

Sine of the input; cosine of the input

Complex exponential of the input

### CORDIC Approximation Method

If you use the CORDIC approximation method (see ), the block input has some further requirements.

When you set Function to , , , or , and set the Approximation method to , the block has these limitations:

• When you use signed fixed-point types, the input angle must fall within the range [–2π, 2π) rad.

• When you use unsigned fixed-point types, the input angle must fall within the range [0, 2π) rad.

When you set Function to and the Approximation method to , the block has these limitations:

• Inputs must be the same size, or at least one value must be a scalar value.

• Both inputs must have the same data type.

• When you use signed fixed-point types, the word length must be or less.

• When you use unsigned fixed-point types, the word length must be or less.

This table summarizes what happens for an invalid input.

Block UsageEffect of Invalid Input
SimulationAn error appears.
Generated codeUndefined behavior occurs. Avoid relying on
undefined behavior for generated code or accelerator modes.
Accelerator modes

## Description

The Simulink Function block is a Subsystem block preconfigured
as a starting point for graphically defining a function with Simulink blocks. The block provides a text interface to function callers. You can
call a Simulink Function block from a Function Caller
block, a MATLAB Function block, or a StateflowChart.

For a description of the block parameters, see the Subsystem block reference page in the Simulink documentation.

You can visualize Simulink Function calls in the Sequence Viewer. The viewer shows
when calls were made with the argument and the return values. See Sequence Viewer block reference.

### Function Interface

The function interface appears on the face of a Simulink
Function block. Editing the block text adds and deletes Argument Inport blocks and Argument Outport blocks from the function
definition. Editing also sets the Function name parameter
in the Trigger block within the Simulink
Function block.

For example, entering on
Inport block () and one Argument Outport block
() within the subsystem.

When calling a function using a Function Caller block,
the parameter Function prototype in the Function
Caller block must match exactly the function interface you
specify on the Simulink Function block. This match
includes the name of the function and the names of input and output
arguments. For example, the Simulink Function block
and the Function Caller block both use the argument
names and .

When calling a function from a Stateflow transition or
state label, you can use different argument names. For example, the Simulink
Function block uses and arguments
while the Stateflow transition uses and arguments
to call the function.

### Function-Call Subsystems Versus Simulink Function Blocks

In general, a Function-Call Subsystem block provides
better signal traceability with direct signal connections than a Simulink
Function block. While a Simulink Function block
eliminates the need for routing input and output signal lines through
the model hierarchy.

Method of executing/invoking functionTriggered using a signal lineCalled by reference using the function name
Formal input arguments (Argument Inport
blocks) and output arguments (Argument Outport
blocks)
NoYes
Local inputs (Inport block) and outputs
(Outport block)
YesYes

## Description

The If block, along with If Action Subsystem blocks containing
an Action Port block, implements if-else
logic to control subsystem execution. For an example using the If block, see If Action
Subsystems.

## Limitations

The If block has the following limitations:

• It does not support tunable parameters. Values for an or
expression cannot be tuned during a simulation in
normal or accelerator mode, or when running generated code.

To implement tunable if-else expressions, tune the expression outside the
If block. For example, use the Relational
Operator block to evaluate the expression outside of the
If block or add the tunable parameter as an input to the
If block.

• It does not support custom storage classes. See Apply Built-In and Customized Storage Classes to Data Elements (Embedded Coder).

• The If expression and Elseif
expressions cannot accept certain operators, such as , and .

## Ports

### — Values for evaluating logical expressionsscalar | vector

Inputs must have the same data type.
The inputs cannot be of any user-defined type, such as an enumerated
type.

The If block does not directly support fixed-point data
types. However, you can use the Compare To Constant block
to work around this limitation. See Support for Fixed-Point Data Type in
If
Action Subsystems.

The If block does not support a boolean data type. If
you want to input a boolean signal to an If block,
convert the signal to an integer type using the Data Type
Conversion block.

Data Types: | | | | | | |

Specify one input port.

integer

Specify the number of input ports. Block ports are labeled
with a character followed by a number,
, where
equals the number of inputs that you specify.

#### Programmatic Use

 Block Parameter: Type: character vector Values: | Default:

### — Specify logical expression (default) | logical expression

The If Action Subsystem attached to the
port executes when its associated expression
evaluates to true.

Specify sending an action signal on the output port when the
input is greater than
.

logical expression

Specify logical expression. This expression appears on the
if output port.

The expression can include only the operators . Operators such as , and are not allowed. The
expression must not contain data type expressions, for example,
, and must not reference workspace
variables whose data type is other than
or
.

#### Programmatic Use

 Block Parameter: Type: character vector Values: | Default:

### — Specify logical expressionempty (default) | list of logical expressions

The If Action Subsystem attached to an
elseif port executes when its expression evaluates
to and all and
expressions are false.

empty

Logical expressions not specified.

list of logical expressions

Specify a list of logical expressions delimited by commas. The
expressions appear on the If block below the
if port and above the
else port when you select the
Show else condition check box.

Expressions can include only the operators . Operators such as , and are not allowed. The
expressions must not contain data type expressions, for example,
, and must not reference workspace
variables whose data type is other than
or
.

#### Programmatic Use

 Block Parameter: Type: character vector Values: | Default:

### — Control display of else porton (default) | off

When the if port and all elseif
port expressions are , the else
port sends an action signal to execute the attached If Action
Subsystem block.

on

Display else port.

off

Hide else port.

#### Programmatic Use

 Block Parameter: Type: character vector Values: | Default:

### — Control zero-crossing detectionon (default) | off

Control zero-crossing detection.

on

Detect zero crossings.

off

Do not detect zero crossings.

#### Programmatic Use

 Block Parameter: Type: character vector Values: | Default:

## Construction

creates a object for a model.

Create a object
by passing the name of the model as an argument.

Example:

## Properties

MATLAB function to run before the start of the simulation.

### — User stringcharacter array

Brief description of the simulation specified as a character array.

## Methods

Method

Purpose

Set model parameters to be used for a specific simulation through
object.

Set block parameters to be used for a specific simulation through
object.
Set initial state to be used for a specific simulation through object.
Set external inputs for a simulation through
object.
Set variables for a simulation through
object.
Specify a MATLAB function to run before start of each simulation through
object.
Specify a MATLAB function to run after each simulation is complete through
object.
Apply changes to the model specified through a
object.
Validate the contents of the
object.
Load variables from MAT-file into a
object.

## Examples

### Create a Object

This example shows you how to create a object.

Open the
model.

```openExample('simulink/OpenTheModelExample');
open_system('ex_sldemo_househeat');

Create a single object for a
model.

```model = 'ex_sldemo_househeat';
```

### Create an Array of Simulink.SimulationInput Objects

This example shows you how to create an array of objects.

Create an array of objects by
using the loop.

```model = 'vdp';
for i = 10:-1:1
end```

### Set Block Parameters using an Array of Simulink.SimulationInput Objects

This example modifies the block parameters of a model through the
object.

Open the
model.

```openExample('simulink/OpenTheModelExample');
open_system('ex_sldemo_househeat');

Create a object for this
model.

```mdl = 'sldemo_househeat';

Modify block parameter.

`in = in.setBlockParameter('sldemo_househeat/Set Point','Value','300');`

Simulate the model.

`out = sim(in)`

### Use Dataset as External Inputs with a Objects

This example shows how use objects to
set external inputs with objects.

Open the model

```mdl = 'sldemo_mdlref_counter';
open_system(mdl);```

Create a object for this
model.

```t = (0:0.01:10)';
ds = ds.setElement(1, timeseries(5*ones(size(t)),t)); % First element
ds = ds.setElement(2, timeseries(10*sin(t),t)); % Second element
ds = ds.setElement(3, timeseries(-5*ones(size(t)),t)); % Third element
```

Create a object and set the
external inputs

```in = Simulink.SimulationInput('sldemo_mdlref_counter');
in = in.setExternalInput('ds.getElement(1),ds.getElement(2),ds.getElement(3)');```

Add other remaining variables in the expressions. Ensure that the dataset is
also on the object.

Simulate the model.

`in = in.setVariable('ds',ds);`

Simulate the model

`out = parsim(in)`

| | | | | | | | | | | |

## Bode Response of Simulink Model

This example shows how to linearize a Simulink model at the operating point specified in the model using the Model Linearizer.

```mdl = 'watertank';
open_system(mdl)```

The Water-Tank System block represents the plant in this control
system and includes all of the system nonlinearities.

To specify the portion of the model to linearize, first open the Linearization tab. To do so, in the Simulink window, in the Apps gallery, click
Linearization Manager.

To specify an analysis point for a signal, click the signal in the model. Then, on
the Linearization tab, in the Insert
Analysis Points gallery, select the type of analysis point.

• Configure the output signal of the PID Controller block as
an Input Perturbation.

• Configure the output signal of the Water-Tank System block
as an Open-loop Output. An open-loop output
point is an output measurement followed by a loop opening, which removes the
effects of the feedback signal on the linearization without changing the
model operating point.

When you add linear analysis points, the software adds markers at their respective
locations in the model. For more information on the different types of analysis
points, see Specify Portion of Model to Linearize.

For more information on defining analysis points in a Simulink model, see Specify Portion of Model to Linearize in Simulink Model. Alternatively, if you do not want to
introduce changes to the Simulink model, you can define analysis points using the Model
Linearizer. For more information, see Specify Portion of Model to Linearize in Model Linearizer.

To open the Model Linearizer for the model, in the Simulink model window, in the Apps gallery, click
Model Linearizer.

To use the analysis points you defined in the Simulink model as linearization I/Os, on the Linear
Analysis tab, in the Analysis I/Os drop-down
list, leave selected.

For this example, use the model operating point for linearization. In the
Operating Point drop-down list, leave selected.

To linearize the system and generate a response plot for analysis, in the
Linearize section, click a response. For this example, to
generate a Bode plot for the resulting linear model, click
Bode.

The software adds the linearized model, , to the
Linear Analysis Workspace and generates a Bode plot for the
model. is the linear model from the specified input to
the specified output, computed at the default model operating point.

For more information on analyzing linear models, see Analyze Results Using Model Linearizer Response Plots.

You can also export the linearized model to the MATLAB workspace. To do so, in the Data Browser, drag
from the Linear Analysis
Workspace to the MATLAB Workspace.

Use strings in multiple applications when you want to pass and manipulate text. For
example, when modeling a fuel control system, instead of using enumerated data to model the
fuel levels, you can use strings like or
. Simulink strings are compatible with MATLAB strings.

Simulink strings are a built-in signal data type. They appear in the Simulink Editor as
(for example, string with maximum
length of N characters) or «string» for strings without maximum length
(dynamic strings). String lengths can range from 1 to 32,766 characters.

Simulink string signals are inherently discrete. If your string signal has a continuous
sample time, the model generates an error at compilation time.

Simulink treats string variables and expressions, such as «a» + «b», the same way it
treats numeric variables. The value of a string variable can be both a character vector and
a MATLAB string. String variables can exist in base, model, and mask workspaces.

String literals are specified with double quotes () or single
quotes (). To be consistent with MATLAB strings, use double quotes. Strings appear on ports and in the
Display block with double quotes.

Simulink strings support 256 characters of the ISO/IEC 8859-1 character set. These
characters are the first 256 code points of Unicode. Simulink does not support the first character («NULL») and
returns an error if the string contains this character.

When a character cannot be displayed, the block stores the actual information and outputs
an escape character with the associated octal value for the character. For example, the
decimal value control character for is 130. The
block displays this control character as the escaped octal . The
ASCII to String block returns as escaped octals characters in the Unicode
set range 0000 to 001F and 007F-009F.

This topic describes how to use strings in Simulink, including:

• A list of available string blocks and blocks particular to string
conversions

• Passing string constants into other string blocks

• Null characters in strings

• Strings with no maximum length

• Interactions with Stateflow

• Generated code

• Limitations

This topic also includes simple examples illustrating how to use string blocks. Examples
in this topic enable the display of block names. To control the display of block names, on
the Format tab, select Auto > Hide Automatic Block Names. For example, you can use string blocks to display and extract coordinate data
and find patterns in strings.

To work with strings in your model, use this table:

ActionBlock

Convert a uint8 vector to a string signal.

Compose an output string signal based on the
Format parameter and input
signals.

Scan an input string and convert it to signals per the format
specified by Format parameter.

Compare two input strings.

Concatenate input strings to form one output string.

Output the string specified by the String
parameter.

Return the index of the first occurrence of the pattern string
sub in the text string
str.

Output the number of characters in the input string.

Convert a string signal to a uint8 vector.

Convert string signal to double signal.

Convert string signal to single signal.

Convert an input string to an enumerated signal.

Extract a substring from a string signal.

Convert the input signal to a string signal.

BlockNotes

Bus Assignment

Nonvirtual and virtual.

Bus Creator

Nonvirtual and virtual.

Bus Selector

Nonvirtual and virtual.

Data Store Memory

Data Store Write

Data Type Duplicate

Display

Display strings with double quotes.

From

From Workspace

Interpolation of data is not supported.

Goto

Ground

Ground value is (empty
string).

Inport

Including root level.

Manual Variant Sink

Manual Variant Source

Manual Switch

No mixed numeric and string types. If there are multiple strings,
the block uses string with the largest size or the dynamic
string.

Merge

Model

Multiport Switch

No mixed numeric and string types. If there are multiple strings,
the block uses string with the largest size or the dynamic
string.

Outport

Including root level.

Probe

Signal Editor

Interpolation of data is not supported.

Signal Specification

Subsystem (all variations)

Switch

No mixed numeric and string types. If there are multiple strings,
the block uses string with the largest size or the
dynamic.

Terminate Function

Terminator

Variant Source

Variant Model

Variant Subsystem

Width

## References

Volder, JE. “The CORDIC
Trigonometric Computing Technique.” IRE Transactions on Electronic
Computers EC-8 (1959); 330–334.

Andraka, R. “A survey of CORDIC algorithm for FPGA based computers.”
Proceedings of the 1998 ACM/SIGDA sixth international symposium on Field
programmable gate arrays. Feb. 22–24 (1998): 191–200.

Walther, J.S. “A Unified Algorithm for Elementary Functions.” Hewlett-Packard
Company, Palo Alto. Spring Joint Computer Conference (1971): 379–386. (from the collection
of the Computer History Museum).
www.computer.org/csdl/proceedings/afips/1971/5077/00/50770379.pdf

## Description

With a MATLAB Function block, you can write a MATLAB function
for use in a Simulink model. The MATLAB function you create
executes for simulation and generates code for a Simulink
Coder target.
If you are new to the Simulink and MATLAB products, see Implementing MATLAB Functions Using Blocks and Create Custom Functionality Using MATLAB Function Block for an overview.

Double-clicking the MATLAB Function block opens its editor,
where you write the MATLAB function, as in this example:

You specify input and output data to the MATLAB Function block
in the function header as arguments and return values. The argument
and return values of the preceding example function correspond to
the inputs and outputs of the block in the model:

You can also define data, input triggers, and function call
outputs using the Ports and Data Manager, which you access from the
MATLAB Function Block Editor by selecting Edit Data.
See Ports and Data Manager.

The MATLAB Function block generates efficient embeddable code based on an analysis that determines the size, class, and complexity of each variable. This analysis imposes the following restrictions:

• The first assignment to a variable defines its, size, class, and complexity.

See Best Practices for Defining Variables for C/C++ Code Generation.

• You cannot reassign variable properties after the initial assignment except when using variable-size data or reusing variables in the code for different purposes.

See Reassignment of Variable Properties.

In addition to language restrictions, the MATLAB Function block supports a
subset of the functions available in MATLAB. A list of supported functions is given in Functions and Objects Supported for C/C++ Code Generation. These functions
include functions in common categories, such as:

• Arithmetic operators like , ,

• Matrix operations like ,
and

• Advanced matrix operations like , , , and

• Trigonometric functions like , , ,
and

### Note

Although the code for this block attempts to produce exactly
the same results as MATLAB, differences might occur due to rounding
errors. These numerical differences, which might be a few initially, can magnify after repeated
operations. Reliance on the behavior of is not recommended. Different C compilers can yield different results for the same computation.

### Note

In the MATLAB Function block, the directive
is included to emphasize that the block’s MATLAB algorithm
is always intended for code generation. The directive,
or the absence of it, does not change the error checking behavior
in the context of the MATLAB Function block. For more
information see Compilation Directive %#codegen.

To support visualization of data, the MATLAB Function block
supports calls to MATLAB functions for simulation only. See Extrinsic Functions to
understand some of the limitations of this capability, and how it
integrates with code analysis for this block. If these function calls
do not directly affect any of the Simulink inputs or outputs,
the calls do not appear in Simulink
Coder generated code.

From MATLAB Function blocks, you can also call
functions defined in a Simulink Function block. You
can call Stateflow functions with Export Chart Level Functions (Make Global) and Allow exported functions to be called by Simulink checked in the chart Properties dialog box.

In the Ports and Data Manager, you can declare a block input
to be a Simulink parameter instead of a port. The MATLAB Function block also supports inheritance of types and size
for inputs, outputs, and parameters. You can also specify these properties
explicitly. See Type Function Arguments, Size Function Arguments,