Pass variable to Matlab-Function Simulink block from Matlab workspace

251 次查看(过去 30 天)
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.

采纳的回答

Rajani Mishra
Rajani Mishra 2019-10-17
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 个评论
Diwakar boya
Diwakar boya 2021-1-29
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by