How can I get the parameters of a MATLAB Function programmatically on command line

조회 수: 23 (최근 30일)
Hello,
I want to get all Inputs with scope 'Parameter' from a MATLAB Function block, but the following commands will only return the Inputs with scope 'Input':
S = sfroot;
B = S.find('Name','myBlockName','-isa','Stateflow.EMChart');
B.Inputs
In the following answer, it is described how to Change Inputs to Parameters so there also must be a way to access these.
Thanks in advance, Philip

답변 (2개)

Roman Luz
Roman Luz 2022년 12월 5일
편집: Roman Luz 2022년 12월 5일
I wanted to access all the MATLAB Functions to find any function parameters that may have unchecked the Tunable checkbox.
I couldn't automate this as I'd hoped. The best I could do was find MATLAB Functions and list their parameter variable names. This turned out to be useful enough in my case, because about 60% of the blocks could be ignored since they didn't have any parameters at all. For the other 40% I still had to manually navigate through each parameter's setting in the Ports and Data Manager GUI.
I wish I could get handles to these parameters and query if they were Tunable.
I'm using R2020a.
mdl_systems = gcs();
search_depth = 99;
find_options = {'FindAll','on', 'LookUnderMasks','all',...
'FollowLinks','on','LookInsideSubsystemReference','on', ...
'LoadFullyIfNeeded','on', 'Variants','ActiveVariants', ...
'IncludeCommented','on', 'SearchDepth',search_depth};
bd = get_param(mdl_systems, "Object");
matlab_subsystems = find_system(mdl_systems, find_options{:}, 'SFBlockType','MATLAB Function');
for b = 1:length(matlab_subsystems)
matlab_subsystem = find(bd, 'Handle',matlab_subsystems(b));
params = get_MATLABFunction_parameters(matlab_subsystem);
end
function params = get_MATLABFunction_parameters(mfun)
childs = mfun.getChildren();
sfun = find(childs, 'BlockType','S-Function');
params = get(sfun, 'Parameters');
end
  댓글 수: 2
Mark Tucker
Mark Tucker 2023년 3월 28일
I couldn’t quite get Roman's code to work but a few modifications allowed me extract all the parameters used by Matlab Functions in a model.
NumberMatlabFunctionParameters = 0;
MatlabFunctionBlocks = find_system(ModelName, 'FollowLinks','on', 'SFBlockType','MATLAB Function');
for ii=1:length(MatlabFunctionBlocks)
MatlabSubsystem = find(slroot, '-isa', 'Stateflow.EMChart', 'Path', MatlabFunctionBlocks{ii});
if ~isempty(MatlabSubsystem)
Children = MatlabSubsystem.getChildren();
for jj=1:length(Children)
if strcmp(Children(jj).Scope,'Parameter')
NumberMatlabFunctionParameters = NumberMatlabFunctionParameters+1;
Parameter{NumberMatlabFunctionParameters} = Children(jj).Name;
end
end
end
end
Parameter = unique(Parameter);

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


Paul
Paul 2023년 3월 28일
For a specific Matlab Function block, as asked in the question (I have a simple Matlab Function block in my model with one input, one output, and one parameter).
S = sfroot;
B = S.find('-isa','Stateflow.EMChart','Path',gcb); % gcb if the block is selected, otherwise use the standard, full path name
C = B.getChildren;
get(C,'Scope')
ans =
3×1 cell array
{'Input' }
{'Output' }
{'Parameter'}
So the third element of C is the parameter and C(3) will reveal its properties.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by