% optPermute() % Generate a set of full factorial permutations based on arguments given as % fields in a structure. % Meant to be used stand-alone or in conjunction with optOverlay() % % optVars = struct('mass', {{5, 10, 15}}, 'pos', {{1, 2}}, 'extra', struct('x', {{2, 3}})) % optSet = optPermute(optVars); function [optSet] = optPermute(optVars) optSet = {}; %Test overlay? % Full factorial experiment fields = fieldnames(optVars); fieldCount = length(fields); % Start recursion optBlank = struct(); doRecursion(optBlank, 1); function doRecursion(optCur, index) if index <= fieldCount field = fields{index}; value = optVars.(field); if iscell(value) % Loop through, concatenate for i = 1:length(value) optCur.(field) = value{i}; % Recurse through the rest of the options doRecursion(optCur, index + 1); end elseif isstruct(value) % Recurse with a new set of options for that given option... % Get the set of options for that set % Incorporate these into permutations optSubSet = optPermute(value); % And like normal for i = 1:length(optSubSet) optCur.(field) = optSubSet{i}; % And so on doRecursion(optCur, index + 1); end end else % End of permute % Add final options to the list of permutations optSet{end + 1} = optCur; end end end