MATLAB: Change parameter name programmatically in stateflow

commandstateflow

Hello, I want to change all the parameter names in stateflow with some script. with the following script I can see the change of parameter name in model explorer:
ChartObj=sfrt.find('-isa','Simulink.BlockDiagram','-and','Name',gcs);
stateChart = ChartObj.find('-isa','Stateflow.Chart');
objArray = stateChart.find('-isa','Stateflow.Data','Scope','Parameter');
set(objArray(i),'Name',new_label);
But the entry in the stateflow still uses the old label name. So when I do ctrl+D, there is error. Can you tell me how to change the entry also with the script? Thanks

Best Answer

function SF_RenameParam(ChartName,ParaOldname,ParaNewname)
rt = sfroot;
m = rt.find('-isa','Simulink.BlockDiagram');
%ch = m.find('-isa','Stateflow.Chart');
ch = m.find('-isa','Stateflow.Chart','Name',ChartName);
%Rename Parameter in workspace
chData = ch.find('-isa', 'Stateflow.Data','-and','Name',ParaOldname);
chData.Name = ParaNewname;
%Rename Parameter name in stateflow transition label
ch_transitions = ch.find('-isa','Stateflow.Transition');
for index=1:length(ch_transitions)
LabelStr = ch_transitions(index).LabelString;
if ismember(ParaOldname,LabelStr)
ch_transitions(index).LabelString = strrep(LabelStr,ParaOldname,ParaNewname);
end
end
end
I hope it's helpful for you (can add find in state),
But sometime it not true for this case: in chart both have ABC and ABC1.
You can modify it follow your ideal
Related Question