MATLAB: How to edit SimMechanics block parameters programmatically

add_blockblock parametersbodyget_paramgroundprogrammatic model generationrevoluteset_paramSimscape Multibodysimulink

I am writing a script to generate a SimMechanics model programmatically and need some help.
bdclose('all') % close all Simulink models
new_system('K') % start a new model called K
open_system('K') % open the new model called K
load_system('mylib') % loads my custom library of SimMechanics blocks (these are just the basic blocks: body, ground, revolute, prismatic, etc.)
For a block like ground, I can add it to the model and change parameters as follows:
add_block('mylib/Ground','K/Ground',...
'CoordPosition','[# # #]',...
'ShowEnvPort','on'); % where [# # #] is an arbitrary location
I can also add the machine environment block and modify it:
add_block('mylib/Machine Environment','K/Machine Environment',...
'Gravity','[0 0 0]');%,...
'Dimensionality','2D Only',...
'AnalysisType','Kinematics',...
'VisualizeMachine','off')
Here is the problem: I cannot seem to properly modify some the Body block parameters
add_block('mylib/Body','K/Body',...
'Mass','0',...
'Inertia','zeros(3)',...
'CGPos','[# # #]',...
'CS1Pos','[# # #]',...
'CS2Pos','[# # #]');
Obviously, replace the '#' with actual numbers. When I run the code, the CS positions are not altered when I double-click on the block in the model. I found that I can change the CG line as follows:
CGstr = ['Left$CG$[# # #]'$WORLD$WORLD$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$false$none'];
and replace…
'CGPos','[# # #]'
in the previous 'add_block' code with
'CG',CGstr
and it works. But I can't do it for the CS1, CS2, etc…Any suggestions?

Best Answer

It looks like you found that string in the model file. There is a similar string for 'WorkingFrames'
'Left$CS1$[0 0 0]$CG$CG$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$true$none#Right$CS2$[0 0 0]$CG$CG$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$true$none'
So I can set
x = 'Left$CS1$[0 0 0]$CG$CG$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$true$none#Right$CS2$[0 0 0]$CG$CG$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$true$none';
and do this
add_block('mblibv1/Bodies/Body','K/Body',... 'Mass','0',... 'Inertia','zeros(3)',... 'CGPos','[0 0 0]',... 'WorkingFrames',x);
but you need to set all your frames in that one big string.
Related Question