How can variables in workspace be used in Simulink function
1 次查看(过去 30 天)
显示 更早的评论
Let say we have a variable defined in workspace
ax=10
I want to use this variable in a Simulink function "abc" defined as:
function y = abc(u)
y=ax*u;
end
Why simulink is showing me an error while executing this function. Alternatively, I used to declare the variable as:
global ax
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169226/image.jpeg)
but it doesn't work.
Can someone help me to solve this issue?
0 个评论
回答(1 个)
KL
2017-11-6
One idea is to take it as an input to your function block and use the "constant block".
Your function could look like,
function y = abc(ax,u)
y=ax*u;
end
Enter ax as the value in your constant block and no need to define it as global.
2 个评论
KL
2017-11-7
Well, If you explain more about your actual problem, it would be easier to find if there's any alternatives to global variables but anyway, using global variables inside matlab function block in simulink is explained in this documentation here.
But be warned about the dangers come with using global variables. If you have a huge scheme, it only gets worse.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!