How do I access base workspace variables from a MATLAB Function Block?

93 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Jun 2023
Edited: MathWorks Support Team on 15 Jun 2023
Due to the differences in how the code within a MATLAB function block interacts with the Simulink solvers, some additional steps must be taken to access base workspace variables from the MATLAB function block. There are a number of options to do so:
1) Use Data Store Memory to import Simulink.Signal objects. By specifying a variable of type Simulink.Signal's scope as 'Data Store Memory' within the MATLAB function block "Ports and Data Manager", you may access the variable from the base workspace.
See the following documentation section for an example of how to do so for a variable in the model workspace. The workflow is identical to reference variables in the base workspace. Keep in mind that the variables must be of type Simulink.Signal and they must follow the rules detailed in the table in the link below:
2) Set the variable's scope to 'Parameter' and pass them in as function arguments to the MATLAB function block. To do this, add the base workspace variables that you wish to use in the MATLAB function block to the list of function arguments. Then, set the scope of these variables to 'Parameter' as described here:
3) Use constant blocks to import the base workspace variables elsewhere in your model and pass them into the MATLAB function block as input arguments. This requires no modification of the variable scope, but increases the number of blocks in your model.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!