- Change the name of the "Interpreted MATLAB Function" block to be "MyFun", so it's easy to reference it in the code.
- Change the Initialization code to be below. The name of the function cannot be passed like in your original code.
- Uncheck the "Evaluate" checkbox for the parameter "select". The value of the parameter "select" will be returned as 'a', or 'b' or 'c'. Otherwise, it will try to "Evaluate" the variable 'a', or 'b', or 'c' and then return the evaluated value to the parameter "select".
Dialog box in the mask for different equations
1 次查看(过去 30 天)
显示 更早的评论
Hi, I want to create a block that can do simple calculation, e.g. u^2, sqrt(u), etc, by selecting the function from the drop down menu
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1365894/image.png)
e.g.,
- if selection equal to a, do u^2
- selection is equal to b, do u + 3
- selection equal to c , do sqrt(u)
switch select
case 'a'
gain = u^2;
case 'b'
gain = u + 3;
case 'c'
gain = sqrt(u);
end
I have added the code to the mask Initialization. When I run the model, I get the error:
Undefined function or variable 'gain'.
Inside the mask is a Fcn block with the parameter gain.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1365899/image.png)
Example code is in the attachment.
0 个评论
采纳的回答
Fangjun Jiang
2023-4-27
编辑:Fangjun Jiang
2023-4-27
A few steps.
switch select
case 'a'
set_param([gcb,'/MyFun'],'MATLABFcn','u^2');
case 'b'
set_param([gcb,'/MyFun'],'MATLABFcn','u+3');
case 'c'
set_param([gcb,'/MyFun'],'MATLABFcn','sqrt(u)');
end
2 个评论
Fangjun Jiang
2023-5-2
编辑:Fangjun Jiang
2023-5-2
check the option "allow library blocks to modify its contents"
更多回答(0 个)
另请参阅
类别
在 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!