MATLAB Answers

1

Use inputs of a user defined Matlab function block as global variables whithin the block

Ali Baradaran 님이 질문을 제출함. 21 Jun 2017
최근 활동 Swarooph 님이 답변함. 23 Jun 2017
In my simulink model there is a Matlab function which consists of many functions:
function block_output = MatFun1(input1, input2, ...)
...
block_output = MatFun2(x, t)
end
function y = MatFun2(x, t)
y = f(x, t, input1, input2, ...)
end
Mathematically MatFun2 is just a function of x and t. Therefore, I would like to keep the notation intact. However, the function requires the block's inputs as well. I tried to globalize the variables input1, input2, ... to be able to use them in MatFun2 as well:
function block_output = MatFun1(input1, input2, ...)
global block_inputs
block_inputs = [input1, input2, ...]
...
block_output = MatFun2(x, t)
end
However, I encounter the following error:
Global declaration not resolved to a Data Store Memory block registered via the Ports and Data Manager.
As I mentioned, I do not want to pass input1, input2, ... to MatFun2 as arguments. Moreover, I want these variables to be known just within this block (not the whole simulink model). Nested functions are also not desirable. What options do I have and how can I resolve the problem?

  댓글 수: 4

표시 이전 댓글 수: 1
Ali Baradaran 2017 年 6 月 23 日
Dear Swarooph,
x and t are persistent variables whose initial values are known within this block. They evolve inside the block. MatFun1 is the main function and the inputs of the block are the arguments of this function. However, these inputs are undefined inside MatFun2.
Please find attached an example which resembles my model.
Swarooph
2017 年 6 月 23 日
Good news: I was able to make the error disappear. Please find the modified model attached. I declared a Data Store Memory called inputs in the Edit Data dialog of MATLAB function.
And I also put in a Data Store Memory block in the Simulink model referring to the same.
Running this model does not error anymore.
Not so good news: In my experience of using Simulink, it seems like you are trying to model something in MATLAB that is perhaps easier to just do in Simulink itself using other blocks.. Of course I don't have the big picture and maybe you have come to this design pattern after a lot of research. I am just guessing here. But the path you are set on doesn't seem very promising to me with MATLAB functions, global variables and such. Just my 2 cents.
Ali Baradaran 2017 年 6 月 23 日
Dear Swarooph,
Thanks for your reply. Since you have commented under my question (instead of answering it), I am not able to vote for it. But thanks again. Regarding your tip, I will reconsider my design.

로그인 to comment.

답변 수: 1

回答者: Swarooph
2017 年 6 月 23 日
 採用された回答

This documentation shows how to do this: How to Use Data Store Memory Blocks
Specific fix to the question in the comments to the question.

  댓글 수: 0

로그인 to comment.



Translated by