MATLAB Function Block Editor
Create, edit, and debug MATLAB code in MATLAB Function blocks
Description
The MATLAB Function Block Editor allows you to view, edit, and create the MATLAB® functions for a MATLAB Function block. When you update the MATLAB code, the software updates the MATLAB Function block with input and output ports that match the input and output arguments in the MATLAB function. The MATLAB Function Block Editor opens in the Simulink Editor.
Open the MATLAB Function Block Editor
Double-click a MATLAB Function block.
Select a MATLAB Function block and, in the Function tab, click Open in Simulink®.
Examples
Open and Use the MATLAB Function Block Editor
Add a MATLAB Function to your model to open the MATLAB Function Block Editor.
In the Simulation tab, click Library Browser. Select Simulink > User-Defined Functions. Drag a MATLAB Function block to the Simulink canvas.
Double-click the block to open the MATLAB Function Block Editor. Replace the function body code with the following:
y = 2*u;
Return to the model by clicking the Up to Parent button . Add a Sine Wave block as the input signal and add a Scope block to capture the input and output of the MATLAB Function block.
Run the simulation and open the Scope block to see the effects of the code.
Redefine the Function Header
Rename the default function and adjust function arguments to automatically update the block inports, outports, and block icon.
Create a new MATLAB Function block and open the MATLAB Function Block Editor.
In the function header, change the text to:
function [x y] = test_function(u1, u2)
Return to the model window. The name on the block icon is now
test_function
, and the block has two input ports for u1 and u2, and two output ports x and y.
Add Variables with the Symbols Pane
Since R2022a
Add function arguments and content to the MATLAB Function block by using the Symbols pane.
Create a new MATLAB Function block and double-click the block to open the MATLAB Function Block Editor.
Open the Symbols pane and Property Inspector. In the Function tab, click Edit Data.
In the Symbols pane, click the Create Data button .
Select the new data and adjust the properties in the Property Inspector.
For more information, see Create and Define MATLAB Function Block Variables.
Add Breakpoints to Debug a Function
You can add breakpoints to debug MATLAB Function blocks.
Create a new MATLAB Function block and open the MATLAB Function Block Editor.
Enter the following code:
function [mult,add,subtract] = number_operations(u1,u2) add = u1 + u2; subtract = u1 - u2; mult = u1 * u2;
Click line numbers 3 and 4 to add two breakpoints. The line numbers highlight in red.
Run the simulation. The simulation stops at the first breakpoint. Click the Continue button to simulate until the next breakpoint. If the last breakpoint has been reached, click Continue to advance to the next time step.
Investigate Code by Running a Report
Run a report on your MATLAB function code to find information on variables and help detect warnings and errors.
Create a new MATLAB Function block and open the MATLAB Function Block Editor.
Enter the following code:
function [mult,add,subtract] = number_operations(u1,u2) add = u1 + u2; subtract = u1 - u2; mult = u1 * u2;
Click the Function Report button .
The report lists variables in the Variables tab at the bottom. Click any of the variable names to highlight instances where the variable appears in the code.
In the function code, point to or click any variable or expression to see a dialog box with more information.
Related Examples
Parameters
Edit Data
— Opens the Symbols pane and Property Inspector
button
Since R2022a
Opens the Symbols pane and the Property Inspector. Click the button to define function variables and edit properties.
Go To
— Goes to specified code
button
Goes to specified code, including functions and line numbers.
Find
— Finds and replaces code in function
button
Finds specified text written in the MATLAB Function Block Editor. You can also use the Ctrl+F keyboard shortcut. If you want to find and replace code, use the Ctrl+H keyboard shortcut. This parameter has the same functionality as the MATLAB Editor. For more information, see Find and Replace Text in Files and Go to Location.
Refactor
— Converts selected code to a function
button
Converts selected code to a function. Use this feature on code that is not already contained in a function. You can create a separate file for the function code or you can convert code into a local function.
To convert code and save it in a separate m-file, select the code and click Refactor > Convert to Function. The code saves as a m-file and the MATLAB Function Block Editor calls the code by the name you specify. To run the model without error, the m-file must be on the path.
To convert code into a local function, select the code and click Refactor > Convert to Local Function. The MATLAB Function Block Editor creates a local function at the bottom of the code and replaces the selected code with a function call to the local function.
Comment
— Comments out code
button
Comments out code. Select the lines you want to comment and click the Comment button
. The button adds one %
to the
selected lines each time you press it. You can also select the code and use the
Ctrl+R keyboard shortcut.
Uncomment
— Removes comments from code
button
Removes comments from code. Select the lines you want to uncomment and click the
Uncomment button . The button removes one %
from the
selected lines each time you press it. You can also select the code and use the
Ctrl+T keyboard shortcut.
Wrap Comments
— Wraps comments
button
Wraps comments. Select the lines you want to comment and click the Wrap comments button .
The MATLAB Function Block Editor does not wrap comments with:
Code section titles (comments that begin with
%%
).Long contiguous text, such as URLs.
List items (text that begins with
*
or#
) onto the preceding line.
Smart Indent
— Formats code with smart indenting
button
Formats code with smart Indenting. Smart indenting automatically formats code to improve code readability, such as when you use functions or logical statements. To manually deploy smart indenting, select the lines of code and click the Smart Indent button . By default, the MATLAB Function Block Editor automatically uses smart indenting. For more information, see Edit and Format Code.
Increase Indent
— Increases the code indent
button
Increases the code indent by one tab indent. To increase the indent, select the lines of code and click the Increase Indent button .
Decrease Indent
— Decreases the code indent
button
Decreases the code indent by one tab indent if the code is indented. To decrease the indent, select the lines code and click the Decrease Indent button .
Specify fixed-point data
— Builds fi
object constructors
button
Builds fi
object constructors with a user interface. To open the
interface, click the Specify fixed-point data button . Click the arrow next to the icon to select additional
fi
constructors. For more information, see Building fimath Object Constructors in a GUI (Fixed-Point Designer).
This features requires a Fixed-Point Designer™ license.
Update Model
— Updates the model and checks for errors
button
Updates the model and checks for errors. Expand the menu under Update Model to select from two options.
Update Model: Compiles the model and checks for static errors.
Refresh blocks: Updates the model blocks to reflect changes made.
You can also use the Ctrl+Shift+D keyboard shortcut to update the model. For more information, see Simulation Phases in Dynamic Systems.
Stop Time
— Specifies the simulation stop time
10
(default) | scalar
Specifies the simulation stop time.
Simulation Mode
— Changes the simulation mode
menu item
Changes the simulation mode. You can select from Normal, Accelerator or Rapid Accelerator. For more information, see Choosing a Simulation Mode.
Fast Restart
— Enables fast restart
"off"
(default) | "on"
Enables fast restart for model simulation. Fast restart can improve performance by preventing compiling. For more information, see Get Started with Fast Restart.
Step Back
— Steps back in simulation
button
Steps back in simulation. You must enable this option using the Simulation Stepping Options dialog box. If you have not enabled stepping back, the button opens the Simulation Stepping Options dialog box. For more information, see Simulation Stepping Options.
Run
— Runs the simulation
button
Runs the simulation. You can run a single simulation, or run multiple simulations by creating design studies with Multiple Simulations and selecting Run All from the drop down. Adjust the time pacing by selecting Simulation Pacing from the drop down.
Step Forward
— Step forward in simulation
button
Steps forward in simulation. For more information, see How Stepping Through Simulations Works.
Stop
— Stops the simulation
button
Stops the simulation.
Step over
— Steps over the breakpoint during debugging
button
Steps over the breakpoint during debugging in the MATLAB Function block. When you press the button, the debugger steps past function calls and does not enter called functions for line-by-line execution. You can use this button only after simulation stops at a breakpoint.
Step In
— Goes to next line of code during debugging
button
Goes to next line of code during debugging in the MATLAB Function block. If the line calls a local function, step into the first line of the local function. You can use this button only after simulation stops at a breakpoint.
Step out
— Exits line-by-line function execution during debugging
button
Exits line-by-line function execution during debugging of the current function or local function. If in a local function, the debugger continues to the line following the call to this local function. You can use this button only after simulation stops at a breakpoint.
Function Report
— Opens a report
button
Opens a report that contains information about the MATLAB Function. You can use this report to find information about variables and expressions in the code. For more information, see MATLAB Function Reports.
Version History
Introduced in R2011aR2022a: Symbols pane replaces Ports and Data Manager
The Ports and Data Manager has been removed. To manage variables, function call outputs, and input triggers in MATLAB Function blocks, use the Symbols pane and the Property Inspector.
R2021b: MATLAB Function Block Editor in Simulink window
The MATLAB Function Block Editor opens in the same Simulink window as the parent model of the MATLAB Function block. Previously, when you opened a function in the MATLAB Function Block Editor, the editor opened in the MATLAB window.
See Also
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
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)