Skip to main content

parseMomentArms.m


% This function is part of the NMSM Pipeline, see file for full license.
%
% This function makes a list of subdirectories and finds the coordinates in
% the subdirectories to organize the files into a 4D data matrix with
% dimensions matching: (numFrames, numTrials, numMuscles, numJoints)
%
% (string) -> (4D matrix of number)
% returns a 4D matrix of the loaded moment arm trials


function [cells, coordinates] = parseMomentArms(directories, model)
import org.opensim.modeling.Storage
if ~isequal(class(model), 'org.opensim.modeling.Model')
model = Model(model);
end
[firstTrial, coordinates] = ...
parseMuscleAnalysisCoordinates(directories(1), model);
cells = zeros([length(directories) size(firstTrial)]);
cells(1, :, :, :) = firstTrial;
for i=2:length(directories)
cells(i, :, :, :) = parseMuscleAnalysisCoordinates(directories(i), ...
model);
end
end


function [cells, coordinates] = ...
parseMuscleAnalysisCoordinates(inputDirectory, model)
import org.opensim.modeling.Storage
[coordFileNames, coordinates] = ...
findMuscleAnalysisCoordinateFiles(inputDirectory, model);
firstFile = storageToDoubleMatrix(Storage(coordFileNames(1)));
cells = zeros([length(coordFileNames) size(firstFile)]);
cells(1, :, :) = firstFile;
for i=2:length(coordFileNames)
cells(i, :, :) = storageToDoubleMatrix(Storage(coordFileNames(i)));
end
end

function [names, coordinates] = ...
findMuscleAnalysisCoordinateFiles(directory, model)
files = dir(directory);
names = string([]);
coordinates = string([]);
for i=0:model.getCoordinateSet().getSize()-1
for j=1:length(files)
if(contains(files(j).name, strcat("MomentArm_", ...
model.getCoordinateSet().get(i).getName().toCharArray', ...
".sto")))
names(end+1) = fullfile(directory, files(j).name);
coordinates(end+1) = model.getCoordinateSet().get(i).getName().toCharArray';
end
end
end
end