How can I get the parameters of a MATLAB Function programmatically on command line
25 次查看(过去 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
0 个评论
回答(2 个)
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
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
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 Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!