Home > functions > internal > dsMergeVarieds.m

dsMergeVarieds

PURPOSE ^

[data_new, variedname_merged, varied_vals ] = mergeVarieds(data,varied_fields)

SYNOPSIS ^

function [data_new, variedname_merged, varied_vals ] = dsMergeVarieds(data,varied_fields,maxchars,varargin)

DESCRIPTION ^

 [data_new, variedname_merged, varied_vals ] = mergeVarieds(data,varied_fields)

 Purpose: This function takes in the current DynaSim datastructure, data, and
 returns data_new. data_new is the same as the original, except the
 fields listed in varied_fields are merged together. This is useful
 for 

 Usage:
   [data_new, variedname_merged, varied_vals ] = mergeVarieds(data,varied_fields)

 Inputs:
   data: DynaSim data structure
   varied_fields: cell array of field names that will be merged
   together

 Outputs:
   data_new: DynaSim output structure
   variedname_merged: New varied fieldname assigned to the merge varied fields
   varied_vals: New values assigned to the merge varied fields

 Examples:
   [data, variedname_merged, varied_vals ] = mergeVarieds(data,vary_labels(linked_inds{j}));
   (see ds2mdd for example)
 
 Submodules: cat_with_underscores

 Author: David Stanley, Boston University, 2017

 See also: unmergeVarieds, modifications2Vary, vary2Modifications

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [data_new, variedname_merged, varied_vals ] = dsMergeVarieds(data,varied_fields,maxchars,varargin)
0002     % [data_new, variedname_merged, varied_vals ] = mergeVarieds(data,varied_fields)
0003     %
0004     % Purpose: This function takes in the current DynaSim datastructure, data, and
0005     % returns data_new. data_new is the same as the original, except the
0006     % fields listed in varied_fields are merged together. This is useful
0007     % for
0008     %
0009     % Usage:
0010     %   [data_new, variedname_merged, varied_vals ] = mergeVarieds(data,varied_fields)
0011     %
0012     % Inputs:
0013     %   data: DynaSim data structure
0014     %   varied_fields: cell array of field names that will be merged
0015     %   together
0016     %
0017     % Outputs:
0018     %   data_new: DynaSim output structure
0019     %   variedname_merged: New varied fieldname assigned to the merge varied fields
0020     %   varied_vals: New values assigned to the merge varied fields
0021     %
0022     % Examples:
0023     %   [data, variedname_merged, varied_vals ] = mergeVarieds(data,vary_labels(linked_inds{j}));
0024     %   (see ds2mdd for example)
0025     %
0026     % Submodules: cat_with_underscores
0027     %
0028     % Author: David Stanley, Boston University, 2017
0029     %
0030     % See also: unmergeVarieds, modifications2Vary, vary2Modifications
0031     %
0032     
0033     % auto_gen_test_data_flag argin
0034     options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0035     if options.auto_gen_test_data_flag
0036         varargs = varargin;
0037         varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0038         varargs(end+1:end+2) = {'unit_test_flag',1};
0039         argin = [{vary_lengths}, {data_length}, varargs]; % specific to this function
0040     end
0041 
0042     
0043     if nargin < 3
0044         maxchars = 25;      % Warnimg - max char length for field name is 63; so this needs to be <= 63
0045     end
0046 
0047     varied_vals = cell(length(data),length(varied_fields));
0048     for i = 1:length(data)
0049         data_temp = data(i);
0050         variedname_merged = cat_with_underscores(varied_fields);
0051         variedname_merged = strcat('C_',variedname_merged);
0052         variedname_merged = cropname(variedname_merged, maxchars);
0053         
0054         for j = 1:length(varied_fields)
0055             
0056             varied_vals{i,j} = data_temp.(varied_fields{j});       % Store varied value. Storead as varied x sim number
0057             data_temp = rmfield(data_temp,varied_fields{j});         % Remove original fields
0058         end
0059         
0060         varied_str{i} = cellfun(@(x) num2str(x),varied_vals(i,:),'UniformOutput',0);
0061         varied_str{i} = cat_with_underscores(varied_str{i});
0062         varied_str{i} = varied_str{i}(1:min(end,maxchars));
0063         data_temp.(variedname_merged) = varied_str{i};
0064         
0065         [~, inds] = intersect(data_temp.varied,varied_fields);
0066         inds2 = true(1,length(data_temp.varied));
0067         inds2(inds) = false;
0068         data_temp.varied = data_temp.varied(inds2);
0069         data_temp.varied{end+1} = variedname_merged;
0070         
0071         data_new(i) = data_temp;
0072     end
0073     
0074     
0075     % auto_gen_test_data_flag argout
0076     if options.auto_gen_test_data_flag
0077         argout = {data_new, variedname_merged, varied_vals}; % specific to this function
0078         dsUnitSaveAutoGenTestDataLocalFn(argin, argout); % localfn
0079     end
0080 
0081 end
0082 
0083 function str_out = cat_with_underscores(cellstr_in)
0084 % Takes in a cell array of chars and concatenates them together with
0085 % underscores separating the original divisions between cells. E.g.
0086 % {'cat','dog'} becomes 'cat_dog'
0087     
0088     temp = vertcat(cellstr_in(:)', repmat({'_'},1,length(cellstr_in)));
0089     temp = temp(:)';
0090     str_out = horzcat(temp{1:end-1});
0091 end
0092 
0093 function out = cropname(in, maxchars)
0094 
0095     if length(in) > maxchars
0096         out = strcat(in(1:maxchars-3),'___');
0097     else
0098         out = in;
0099     end
0100     
0101 end

Generated on Tue 12-Dec-2017 11:32:10 by m2html © 2005