Page 1 of 1

Updating Model State in Matlab

Posted: Wed Feb 16, 2022 9:13 am
by hicksbrody
Hello,

I using the StatesReporter Analysis tool to export the model states for some data I am looking at.

I am then trying to load my model into Matlab, and assign the states exported from the StatesReporter, then loop through the states for each time step and perform some calculations at that state.

Does anyone know how to do this?

Thanks,
Brody

Re: Updating Model State in Matlab

Posted: Wed Feb 16, 2022 2:27 pm
by jbanks
https://simtk.org/projects/emg_opt_tool
The EMGopt_OS40.m script from this project should give you a general idea of the procedure. Let me know if you have any questions.
Jake Banks

Re: Updating Model State in Matlab

Posted: Wed Feb 16, 2022 5:24 pm
by nicos1993
Hello Brody,

I have taken some of the code you posted in relation to a previous post linked to this matter. I think that should do the trick!

states_sto = Storage(statesPath);

stateTrajectory = StatesTrajectory.createFromStatesStorage(model,states_sto);
nRows = stateTrajectory.getSize();

% Assuming your model is called model
myState = model.initSystem();


calcn_r = model.getBodySet.get('calcn_r'); %get body reference

for n = 0:nRows-1
state = stateTrajectory.get(n); %now I have a reference to the current state
% Populate state vector for each loop iteration
model.setStateVariables(myState,state);

model.realizePosition(state);
rotation = calcn_r.getTransformInGround(state).R();
quat = rotation.convertRotationToQuaternion();
calcn_rotation(n+1,:) = [quat.get(0) quat.get(1) quat.get(2) quat.get(3)];
end