function [s, sIdx] = pnExtractField(data,fieldName) %PNEXTRACTFIELD Extract data for a single variable % [ s, sIdx ] = pnExtractField(data,fieldName) extracts only the given % field name for the data provided. The data is assumed to be in one of % two forms: a cell array with each row as an individual field or a % structure with each observation as an individual field (see below). % The output, s, is in the same format, with all data but the desired % field removed. % sIdx are the indices used to extract this field from the original data. % % % Inputs: % data - Cell array of data inputs in standard format: % Column 1 - Subject IDs % Column 2 - Time stamp vectors for each subject % Column 3 - Feature name vectors for each subject % Column 4 - Data value vectors for each subject % Empty cells indicate no available measurements (missing data). % % OR % % data - Structure with each field being a new observation % Each field should contain a cell array of the following format: % Column 1 - Time stamp vectors for each subject % Column 2 - Feature name vectors for each subject % Column 3 - Data value vectors for each subject % % % Outputs: % s - Cell array of only fieldName data in standard format: % Column 1 - Subject IDs % Column 2 - Time stamp vectors for each subject % Column 3 - Feature name vectors for each subject (only contains % 'PaFi') % Column 4 - Data value vectors for each subject % Empty cells indicate no available measurements (missing data). % % OR % % s - Structure with each field being a new observation % Each field containS a cell array of the following format: % Column 1 - Time stamp vectors for each subject % Column 2 - Feature name vectors for each subject (only % fieldName will be present) % Column 3 - Data value vectors for each subject % % sIdx - Cell array of indices used for each observation to extract % the data. % % Example % %=== Load data in % load('data_processed_cell.mat'); % % %=== Extract pH % [ pH ] = pnExtractField(data,'pH'); % % See also PNLOADTEXTFILESCELL PNMAIN % Copyright 2012 Alistair Johnson % $LastChangedBy: alistair $ % $LastChangedDate: 2012-05-29 09:01:11 -0400 (Tue, 29 May 2012) $ % $Revision: 1 $ % Originally written on PCWIN64 by Alistair Johnson, 07-Mar-2012 18:16:23 % Contact: alistairewj@gmail.com if isstruct(data) %=== Create structure containing indices of each parameter fn = fieldnames(data); sIdx = structfun(@(x) strcmp(x(:,2),fieldName),data, 'UniformOutput',false); s = struct(); for k=1:length(fn) s.(fn{k}) = data.(fn{k})(sIdx.(fn{k})); end else %=== Cell array format sIdx = cellfun(@(x) strcmp(x,fieldName), data(:,3), 'UniformOutput',false); s=[data(:,1), cellfun(@(x,y) x(y), data(:,2:end), repmat(sIdx,1,3),'UniformOutput',false)]; end end