Documentation Center

  • Trial Software
  • Product Updates

mech_set_states

Populate SimMechanics states in Simulink state vector

Syntax

X = mech_set_states(vector_mgr)
X = mech_set_states(vector_mgr, X)
X = mech_set_states(vector_mgr, X, mech_states)

Description

X = mech_set_states(vector_mgr) returns the Simulink® state vector X for the model associated with vector_mgr. The state vector entries corresponding to the SimMechanics™ states are filled with the values specified in vector_mgr. Entries in X that do not correspond to SimMechanics states are set to their initial values, as reported by Simulink.

X = mech_set_states(vector_mgr, X) overwrites the entries that correspond to SimMechanics states in the input state vector X with the values specified in vector_mgr. Entries of X that do not correspond to SimMechanics states are left unchanged. Specify [] in place of X to obtain the initial values for the nonmechanical states, as reported by Simulink.

X = mech_set_states(vector_mgr, X, mech_states) overwrites the entries that correspond to SimMechanics states in the input state vector X with the values specified in mech_states (for example, as reported by vector_mgr.X). Entries of X that do not correspond to SimMechanics states are left unchanged.

Input Arguments

mech_set_states accepts one, two, or three inputs.

vector_mgr

An instance of the object class MECH.StateVectorMgr corresponding to the machine. This input is required.

X

A vector of Simulink state values. This input is optional.

mech_states

A vector of the mechanical state values, assigned to vector_mgr.X. This input is optional and requires the second optional input, X.

Output Arguments

X

The Simulink state vector for the model you are querying, with the mechanical state values set to the values specified by the function.

Examples

Setting Mechanical State Values

Set state values in a simple pendulum model:

load_system('mech_spen');
set_param('mech_spen','InitInArrayFormatMsg','None');
vm = mech_stateVectorMgr('mech_spen/Machine Environment');
vm.x = [pi/4 0];
simulinkState = mech_set_states(vm);

Simulate, capturing the model state over the entire simulation:

simOutIS = sim('mech_spen','InitialState','simulinkState','LoadInitialState','on',...
   'StopTime','5','SaveState','on','StateSaveName','xoutIS');
xoutIS = simOutIS.get('xoutIS');

Compare the initial condition xoutIS(1,:) with the initial condition obtained without setting the initial state in xout0(1,:):

simOut0 = sim('mech_spen','StopTime','5','SaveState','on',...
   'StateSaveName','xout0');
xout0 = simOut0.get('xout0');

See Also

| | | | |

Was this topic helpful?