Skip to main content

writeMuscleTendonPersonalizationOsimxFile.m


% This function is part of the NMSM Pipeline, see file for full license.
%
% This function prints out the optimized muscle tendon parameters in an
% osimx file
%
% (string, 2D matrix, string) -> (None)
% Prints MuscleTendonPersonalization results in osimx file


function writeMuscleTendonPersonalizationOsimxFile(modelFileName, ...
osimxFileName, optimizedParams, muscleNames, results_directory)
model = Model(modelFileName);

if isfile(osimxFileName)
osimx = parseOsimxFile(osimxFileName);
[~, name, ~] = fileparts(osimxFileName);
outfile = fullfile(results_directory, strcat(name, "_mtp.xml"));
else
osimx = buildMtpOsimxTemplate(...
replace(model.getName().toCharArray',".","_dot_"), ...
modelFileName);
[~, name, ~] = fileparts(modelFileName);
outfile = fullfile(results_directory, strcat(name, "_mtp.xml"));
end
osimx.modelName = name;
osimx.model = modelFileName;
for i = 1:length(muscleNames)
muscleParams = makeMuscleParams(model, muscleNames(i), optimizedParams, i);
osimx.muscles.(muscleNames(i)) = muscleParams;
end

writeOsimxFile(buildOsimxFromOsimxStruct(osimx), outfile)
end

function params = makeMuscleParams(model, muscleName, optimizedParams, index)
if isfield(optimizedParams, 'electromechanicalDelays')
params.electromechanicalDelay = optimizedParams.electromechanicalDelays(index);
end
if isfield(optimizedParams, 'activationTimeConstants')
params.activationTimeConstant = optimizedParams.activationTimeConstants(index);
end
if isfield(optimizedParams, 'activationNonlinearityConstants')
params.activationNonlinearityConstant = ...
optimizedParams.activationNonlinearityConstants(index);
end
muscle = model.getForceSet().getMuscles().get(muscleName);
if isfield(optimizedParams, 'emgScaleFactors')
params.emgScaleFactor = optimizedParams.emgScaleFactors(index);
end
if isfield(optimizedParams, 'optimalFiberLengthScaleFactors')
params.optimalFiberLength = muscle.get_optimal_fiber_length() * ...
optimizedParams.optimalFiberLengthScaleFactors(index);
end
if isfield(optimizedParams, 'tendonSlackLengthScaleFactors')
params.tendonSlackLength = muscle.get_tendon_slack_length() * ...
optimizedParams.tendonSlackLengthScaleFactors(index);
end
if isfield(optimizedParams, 'maxIsometricForce')
params.maxIsometricForce = optimizedParams.maxIsometricForce(index);
end
end