Pass variable to Matlab-Function Simulink block from Matlab workspace

214 views (last 30 days)
Hello there,
I would like to define certain variable, say initial_position, and pass it to the MATLAB Function block as an input argument. Moreover, I want Simulink (or rather the body of MATLAB Function block) to guess that this variable is defined in the workspace, so that I pass only other arguments via input terminals.
To put this more clearly, I would like to define MATLAB Function block, which will begin as follows
function out = my_MATLAB_Function(arg1, arg2, initial_position)
%% ...
end
where my MATLAB Function block will have only two input terminals for arg1 and arg2, and initial_position is only defined within workspace.
In the documentation for MATLAB Function block, under n - input argument field, we have the following message:
When you add the argument in the editor, the block adds the port correspondingly.
I am sure, however, that there is more to it, as I have access to simulink model, which does exactly what I pictured above, yet I lack the knowledge how to set it.

Accepted Answer

Rajani Mishra
Rajani Mishra on 17 Oct 2019
Hi,
Parameter arguments for MATLAB Function blocks do not take their values from signals in the Simulink model. Simulink searches up the workspace hierarchy. Simulink first looks in a masked workspace if the MATLAB Function block or a parent subsystem is masked. If the value is not found, it next looks in the model workspace and then the MATLAB base workspace.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. For more information refer to the following link: https://www.mathworks.com/help/simulink/ug/using-global-data-with-the-matlab-function-block.html#bsgs3fa-1
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:https://www.mathworks.com/help/simulink/ug/parameter-arguments-in-matlab-function-block-functions.html
Hope this helps!
  4 Comments
Diwakar boya
Diwakar boya on 29 Jan 2021
can any one help ,while implementing the second method I am getting an error
' An already deleted object cannot be used. '
I am trying to pass a rectangle as argument

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!