フィルターのクリア

Simulink Matlab Function block

84 ビュー (過去 30 日間)
Jeremy
Jeremy 2013 年 12 月 17 日
コメント済み: Sunandan Adhikary 2021 年 2 月 7 日
I have a Matlab workspace variable called clock_frequency.
I use this variable as a parameter in many of my simulink blocks.
I want to be able to access and alter this variable within my Simulink Matlab Function block, but I cannot do it successfully. Unlike the other simulink blocks (that can see the workspace variables) the matlab function block cannot.
I do not want to create a port and pass the variable in as a signal for various reasons.
Reading through the documentation, I have to use data store memory, but this doesn't seem to work. I got a partial version of this working, and the simulink model began running really slowly.

採用された回答

Niklas Nylén
Niklas Nylén 2013 年 12 月 18 日
This function will read the value x from the base workspace, add 3 to it and write back the new value to the workspace as x. On the next iteration it will read the new value of x and so on.
function y = fcn()
%#codegen
coder.extrinsic('evalin', 'assignin')
x = 0; % Needed in order to set variable class correctly
x = evalin('base','x'); % Read value from workspace
y = x+3;
assignin('base','x',y); % Assign value back to workspace
  3 件のコメント
Sinan Ibrahim Bayraktar
Sinan Ibrahim Bayraktar 2019 年 3 月 16 日
I get an error while using it for a maxtrix. Is there a way to access this matrix even if I don't want to change it?
N/A
N/A 2020 年 10 月 13 日
I believe you could define the matrix with zeros before using 'evalin'. For instance, if you want to import a matrix 'x' that is 2X3 in size, define the following in place of fourth line above.
x=zeros(2,3);

サインインしてコメントする。

その他の回答 (2 件)

Rubem Pacelli
Rubem Pacelli 2019 年 8 月 1 日
編集済み: Rubem Pacelli 2019 年 8 月 1 日
There are another solution for your question.
  1. Double-click in the MATALAB function block. Will open a script function of your "MATALAB function" block
  2. click on edit data
  3. click on add -> data (or just type cntrl+D shortcut keyword)
  4. type the same name of the variable tha you want pass to MATLAB function block
  5. In Scope, choose "Parameter" option.
It's work fine in my computer. You need save your variable in the MATLAB workspace.
  5 件のコメント
mohammad nojavan
mohammad nojavan 2020 年 8 月 20 日
hi rubem i dowloaded a simulink file from internet tath contaies some of " Data store Memory block's" . if i want to change the value of this block they dont change and when i run the simulink file the value of data store memory blocks comeback the initial values. please help me
elec.engeen2011@gmail.com
Sunandan Adhikary
Sunandan Adhikary 2021 年 2 月 7 日
This way we n import the variable but it does not get updated! I am trtying to update arrays in a structure and everytime it enters the function it updates that particular indexec position but later when the function is called in next iteration, those values are gone from the array.

サインインしてコメントする。


Ryan Livingston
Ryan Livingston 2013 年 12 月 18 日
You may want to add the variable as an input parameter under the ports and data manager:
It will show up in the function block as an input argument like:
function y = fcn(u)
where 'u' will be the parameter name you choose. Then it will search through the workspaces as described in the second link.
  3 件のコメント
Ali Mukhtar
Ali Mukhtar 2019 年 9 月 12 日
編集済み: Ali Mukhtar 2019 年 9 月 12 日
hi how can access the values of variables written in simulink function block?
Marlon Trujillo
Marlon Trujillo 2020 年 5 月 27 日
This is limited to some types of variables...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by