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

25 views (last 30 days)
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

Answers (2)

Roman Luz
Roman Luz on 5 Dec 2022
Edited: Roman Luz on 5 Dec 2022
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 Comments
Mark Tucker
Mark Tucker on 28 Mar 2023
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);

Sign in to comment.


Paul
Paul on 28 Mar 2023
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.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!