How can I get the parameters of a MATLAB Function programmatically on command line
Afficher commentaires plus anciens
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
Réponses (2)
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 commentaires
Mark Tucker
le 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);
Roman Luz
le 8 Mai 2023
Thanks! This is helpful.
Paul
le 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.
1 commentaire
Roman Luz
le 8 Mai 2023
I finally found the Tunable setting! Thanks for the help.
Catégories
En savoir plus sur MATLAB Function Block Editor dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!