Home > functions > internal > dsData2Table.m

dsData2Table

PURPOSE ^

Converts DynaSim structure to 1D cell array format. Later can use to

SYNOPSIS ^

function [data_table,column_titles,time] = dsData2Table(data,verbose_flag,maxrows)

DESCRIPTION ^

 Converts DynaSim structure to 1D cell array format. Later can use to
 import to MDD

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data_table,column_titles,time] = dsData2Table(data,verbose_flag,maxrows)
0002     % Converts DynaSim structure to 1D cell array format. Later can use to
0003     % import to MDD
0004     
0005     if nargin < 2
0006         verbose_flag = 0;
0007     end
0008     
0009     if nargin < 3
0010         maxrows = 10;
0011     end
0012 
0013     dsCheckData(data);            % Makes sure it's a valid DynaSim Data structure
0014     
0015     % Extract Time variable
0016     time = data(1).time;
0017     
0018     % Create dummy varied variable if none there
0019     for i = 1:length(data)
0020         if ~isfield(data(i),'varied')
0021             data(i).varied = [];
0022         end
0023         if isempty(data(i).varied)
0024             data(i).varied = {'Varied1'};  % Random name for varied data
0025             data(i).Varied1 = i;           % Random value
0026         end
0027     end
0028     
0029     % ## VARIED parameter sweeps ##
0030     varied=data(1).varied;
0031     num_varied=length(varied); % number of model components varied across simulations
0032     num_sims=length(data); % number of data sets (one per simulation)
0033     %param_mat=zeros(num_sims,num_varied); % values for each simulation
0034     
0035     % Create params cell array--just used for making ax
0036 %     for iVar=1:num_varied
0037 %         if isnumeric(data(1).(varied{iVar}))
0038 %             params{iVar} = [data.(varied{iVar})]; % store as nested mat
0039 %         else
0040 % %             for iSim = 1:length(data)
0041 % %                 params{iVar}{iSim} = data(iSim).(varied{iVar}); %store as cells
0042 % %             end
0043 %             params{iVar} = {data.(varied{iVar})}; %store as nested cell array
0044 %         end
0045 %     end
0046     
0047     % ## FIELDS Get all fields of data ##
0048     % Get metadata for all data fields (e.g. populations / currents / state
0049     % variables)
0050     labels = data(1).labels;
0051     labels = labels(cellfun(@isempty,strfind(labels,'time'))); % Remove time from labels
0052     num_labels = length(labels);
0053     
0054     % Determine all unique populations
0055     pop_names={data(1).model.specification.populations.name}; % list of populations
0056     
0057     % Build list populations and variables
0058     separatorInds = strfind(labels,'_');
0059     func1 = @(x,y) x(1:y(1)-1);
0060     func2 = @(x,y) x(y(1)+1:end);
0061     pops = cellfun(func1,labels,separatorInds,'UniformOutput',0);
0062     vars = cellfun(func2,labels,separatorInds,'UniformOutput',0);
0063     
0064     % ## Build a large linear list ##
0065     ind=0;
0066     data_linear = cell(1,num_sims*num_labels);
0067     for iSim = 1:num_sims
0068         for iLabel = 1:num_labels
0069             
0070             ind=ind+1;
0071             data_linear{ind} = data(iSim).(labels{iLabel});
0072             
0073             % Number of parameter sweeps, plus populations, plus variables (Vm, state variables, functions, etc.)
0074             for iVar = 1:num_varied
0075                 if isnumeric(data(1).(varied{iVar}))
0076 %                     ax{iVar}(ind) = params{iVar}(iSim); % using nested mat
0077                     ax{iVar}(ind) = data(iSim).(varied{iVar}); % using nested mat
0078                 else
0079 %                     ax{iVar}{ind} = params{iVar}{iSim}; % using nested cell array
0080                     ax{iVar}{ind} = data(iSim).(varied{iVar}); % using nested cell array
0081                 end
0082             end
0083             
0084             ax{num_varied+1}{ind} = pops{iLabel};
0085             
0086 %             iVar=iVar+1; % REVIEW: I don't think this line does anything
0087             ax{num_varied+2}{ind} = vars{iLabel};
0088             
0089         end
0090     end
0091     
0092     ax_names = varied;
0093     ax_names{num_varied+1} = 'populations';
0094     ax_names{num_varied+2} = 'variables';
0095     
0096     % Transpose everything to make it in terms of columns instead of rows.
0097     data_linear = data_linear(:);
0098     for i = 1:length(ax)
0099         ax{i} = ax{i}';
0100     end
0101     
0102     % Combine everything into one data table
0103     data_table = horzcat({data_linear},ax);
0104     
0105     % List table column names
0106     column_titles = {'data',ax_names{:}};
0107     
0108     if verbose_flag
0109         previewTable(data_table,column_titles,maxrows);
0110     end
0111     
0112 end

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