How do I programmatically add inputs to a matlab function block ?

조회 수: 19 (최근 30일)
Erik Voigt
Erik Voigt 2016년 11월 29일
답변: Igal 2018년 6월 19일
Hi I am creating a utility-function that creates a model with a Matlab-function-block, which content is derived from our internal-developed datadictionary. I figured out how to change the Matlab-code itself via Stateflow.EMChart, but I cant find a solution on how to change the number of inputs (in my case number of outputs is always 1). Does anybody has some experience to share here?
Kind Regards Erik Voigt

채택된 답변

Nirja Mehta
Nirja Mehta 2016년 12월 1일
I am assuming you are working on a newer release of MATLAB, but the following links explain how you can programmatically change the number of inputs of your MATLAB function block (which was called Embedded MATLAB Function block in older releases): https://www.mathworks.com/matlabcentral/answers/100659-how-do-i-set-user-defined-parameters-in-an-embedded-matlab-function-block-from-the-matlab-command-pr
Essentially, you need to get the handle of the Stateflow.EMChart object that you already have handy by executing the following command:
>> B = S.find('Name','myBlockName','-isa','Stateflow.EMChart');  
You would then be able to add extra inputs to the Embedded MATLAB Function block as explained in Step 2. To do this execute the following commands at the MATLAB prompt:
>> line1='function y = fcn(u,param1,param2)';
>> line2=' y = u*param1*param2;';
>> B.Script = line1;
>> B.Script = line2;
  댓글 수: 1
Erik Voigt
Erik Voigt 2016년 12월 7일
편집: Erik Voigt 2016년 12월 7일
Hi Thanks for the answer. Yes this works just fine. As it seems the interface is handled implicit via the script content, Here my prototype for this part so far. kr Erik.
% Add content in Matlab block
S = sfroot;
B = S.find('Name','DerivedCalculation','-isa','Stateflow.EMChart');
% Add Matlab-code
code = sprintf(['function y = fcn(u1 u2 u3) \n' ...
' %%#codegen \n' ...
' y = u1 + u2 + u3; \n' ...
'end' ...
]);
B.Script = code;
% Set datatypes on interface
B.Inputs(1).DataType = 'single';
B.Inputs(2).DataType = 'single';
B.Inputs(3).DataType = 'single';
B.Outputs(1).DataType = 'single';

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

추가 답변 (1개)

Igal
Igal 2018년 6월 19일
Hi All,
Is there any way I can programatically set output sizes as well and not only data types? i can't find anyting about it in documentation.
Thanks.

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by