How to use 'Switch case' with Matlab Function Block in Simulink?
10 次查看(过去 30 天)
显示 更早的评论
I'm able to run the following code in MATLAB:
n = input('n: ');
x = 3;
y = 2;
switch n
case 1
z = (x-y)/2
case 2
z = (x+y)/2
end
I want the 'n' value to come from a constant (or any other input block), which goes to MATLAB Function Block, and display the output when I simulate the Simulink model. Something like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/151638/image.png)
I edited the Function Block's code as follows:
function result = fcn(n)
%#codegen
x = 3;
y = 2;
switch n
case 1
z = (x-y)/2
case 2
z = (x+y)/2
end
Its giving me several errors:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/151639/image.png)
Can someone tell me what is wrong with my code or any other fixes to make this thing work?
Thanking You, Harshil
0 个评论
回答(3 个)
Azzi Abdelmalek
2015-6-26
What if n is different from 1 and 2? Try this
function result = fcn(n)
%#codegen
x = 3;
y = 2;
z=0;
switch n
case 1
z = (x-y)/2
case 2
z = (x+y)/2
end
3 个评论
Azzi Abdelmalek
2015-6-26
I tested this code, it works fine. Post your real code, we can't find the problem with another code.
Anjum Tamboli
2017-11-30
</matlabcentral/answers/uploaded_files/96734/Screenshot%20(29).png>This code does not work. for first range of values output should be only at y and for second range ouput should be at y1. please suggest me the modifications to run the program successfully. Thanks and Regards Anjum
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!