필터 지우기
필터 지우기

Programmatically change MATLAB Fcn Block Function Configuration Script

조회 수: 1 (최근 30일)
Aarish
Aarish 2023년 4월 26일
답변: Aaryan 2023년 5월 9일
I'm hoping to programmatically change the Function Script for a MATLAB Fcn Block
currentScript = 'function y = fcn(u, value)';
config = get_param(mFcnBlock, 'MATLABFunctionConfiguration');
config.FunctionScript = currentScript;
My code is as shown above, and this works as intended. The result is:
function y = fcn(u, value)
How would I change the currentScript to be the following?
function y = fcn(u, value)
switch u
case true
y = value;
case false
y = 0;
otherwise
y = nan;
end
I've tried several ways, but the function script almost always ends up as the following:
function y = fcn(u, value,switch,ucase,truey, value1,case,false,y, otherwise, y1, nan,end)
Thanks!

채택된 답변

Paul
Paul 2023년 4월 27일
Hi Aarish,
This worked for me.
config = get_param(gcb,'MATLABFunctionConfiguration'); % Matlab Fcn block selected in Simulink diagram
new_script = ...
"function y = fcn(u, value)" + newline + ...
"switch u" + newline + ...
" case true" + newline + ...
" y = value;" + newline + ...
" case false" + newline + ...
" y = 0;" + newline + ...
" otherwise" + newline + ...
" y = nan;" + newline + ...
"end";
config.FunctionScript = new_script;
If you don't mind me asking, what is the use case for wholesale change of the function code progrmatically?

추가 답변 (1개)

Aaryan
Aaryan 2023년 5월 9일
how to add a new function to the same block?
like function x = fcn(u,v)

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by