Dialog box in the mask for different equations

조회 수: 1 (최근 30일)
Igor
Igor 2023년 4월 25일
편집: Fangjun Jiang 2023년 5월 2일
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
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.
Example code is in the attachment.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2023년 4월 27일
편집: Fangjun Jiang 2023년 4월 27일
A few steps.
  1. Change the name of the "Interpreted MATLAB Function" block to be "MyFun", so it's easy to reference it in the code.
  2. Change the Initialization code to be below. The name of the function cannot be passed like in your original code.
  3. 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".
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
Igor
Igor 2023년 5월 2일
Thank you for the answer
Do you maybe know how to create a library block from this? When I do it, the selection gets hard-coded, and I cannot apply different option without breaking the link.
Fangjun Jiang
Fangjun Jiang 2023년 5월 2일
편집: Fangjun Jiang 2023년 5월 2일
check the option "allow library blocks to modify its contents"

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Author Block Masks에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by