cell2mat_jd

PURPOSE ^

SYNOPSIS ^

function s = cell2mat_jd(c,dim,numopt,dispmode)

DESCRIPTION ^

 this function builds a structure s from the cell array of structures c
 where each field of s is a concatenation of the compatible arrays from fields c 

   INPUTS : 
       - c : cell array of structures
       - dim : dim can be either : 
           o the dimension along which to concatenate fields of c (default : 2 for columns) 
           o the field defining the number of new elements from each cell
       - numopt : restrict to numerical arrays
       - dispmode : disply warnings

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function s = cell2mat_jd(c,dim,numopt,dispmode)
0002     %
0003     % this function builds a structure s from the cell array of structures c
0004     % where each field of s is a concatenation of the compatible arrays from fields c
0005     %
0006     %   INPUTS :
0007     %       - c : cell array of structures
0008     %       - dim : dim can be either :
0009     %           o the dimension along which to concatenate fields of c (default : 2 for columns)
0010     %           o the field defining the number of new elements from each cell
0011     %       - numopt : restrict to numerical arrays
0012     %       - dispmode : disply warnings
0013     %
0014     if nargin < 4,
0015         dispmode = false;
0016     end
0017     %
0018     if nargin < 3 || ~islogical(numopt),
0019         numopt = false;
0020     end
0021     %
0022     if nargin < 2,
0023         dim = 2;
0024     end
0025     %
0026     s = struct;
0027     %
0028     if ~iscell(c) || isempty(c),
0029         disp_jd(dispmode,'WARNING : c must be a non-empty cell array');
0030         return
0031     else
0032         c = c(:);
0033     end
0034     %
0035     if ~isstruct(c{1}),
0036         disp_jd(dispmode,'WARNING : c must be a cell array of structures');
0037         return
0038     else
0039         s = c{1};
0040     end
0041     %
0042     sfd = fieldnames(s);
0043     nsfd = length(sfd);
0044     mask = false(1,nsfd);
0045     %
0046     if numopt,
0047         for isfd = 1:nsfd,
0048             mask(isfd) = ~isnumeric(s.(sfd{isfd}));
0049             if mask(isfd),
0050                 disp_jd(dispmode,['WARNING : field ''',sfd{isfd},''' is not numeric. Discarded.']);
0051             end
0052         end
0053     end
0054     %
0055     if ischar(dim),
0056         if isfield(s,dim),
0057             snumel = numel(s.(dim));
0058             dim = ones(1,nsfd);
0059             %
0060             for isfd = 1:nsfd,
0061                 szs = size(s.(sfd{isfd}));
0062                 iszs = find(szs == snumel);
0063                 if isempty(iszs),
0064                     disp_jd(dispmode,['WARNING : field ''',sfd{isfd},''' has no dimension with ',num2str(snumel),' elements. Discarded.']);
0065                     mask(isfd) = true;
0066                 elseif length(iszs) > 1,
0067                     disp_jd(dispmode,['WARNING : field ''',sfd{isfd},''' has more than one dimension with ',num2str(snumel),' elements. Lowest dimension enforced.']);
0068                     dim(isfd) = iszs(1);
0069                 else
0070                     dim(isfd) = iszs;
0071                 end
0072             end
0073         else
0074             dim = repmat(2,[1,nsfd]);
0075         end
0076     else
0077         dim = repmat(dim,[1,nsfd]);
0078     end
0079     %
0080     nc = length(c);
0081     %
0082     for ic = 2:nc,
0083         if ~isstruct(c{ic}),
0084             disp_jd(dispmode,['WARNING : c{',num2str(ic),'} is not a structure. Discarded.']);
0085             continue
0086         end
0087         %
0088         fd = fieldnames(c{ic});
0089         nfd = length(fd);
0090         %
0091         for ifd = 1:nfd,
0092             if ~isfield(s,fd{ifd}),
0093                 disp_jd(dispmode,['WARNING : field ''',fd{ifd},''' is not present in all structures of cell array. Discarded.']);
0094             else
0095                 isfd = strcmp(sfd,fd{ifd});%index is s field array
0096                 %
0097                 if mask(isfd),
0098                     continue
0099                 end
0100                 %
0101                 if numopt && ~isnumeric(c{ic}.(fd{ifd})),
0102                     disp_jd(dispmode,['WARNING : field ''',fd{ifd},''' is not numeric in all structures of cell array. Discarded.']);
0103                     mask(isfd) = true;
0104                     continue
0105                 end
0106                 %
0107                 szs = size(s.(sfd{isfd}));
0108                 szc = size(c{ic}.(fd{ifd}));
0109                 %
0110                 if length(szs) ~= length(szc) || any(szs([1:dim(isfd)-1,dim(isfd)+1:end]) ~= szc([1:dim(isfd)-1,dim(isfd)+1:end])),
0111                     disp_jd(dispmode,['WARNING : field ''',fd{ifd},''' cannot be concatenated between all structures of cell array. Discarded.']);
0112                     mask(isfd) = true;
0113                     continue
0114                 end
0115                 %
0116                 s.(sfd{isfd}) = shiftdim([shiftdim(s.(sfd{isfd}),dim(isfd)-1);shiftdim(c{ic}.(fd{ifd}),dim(isfd)-1)],length(szs) - dim(isfd) + 1);
0117                 %
0118             end
0119         end
0120     end
0121     %
0122     s = rmfield(s,fd(mask));
0123     %
0124 end
0125 %
0126 function disp_jd(dispmode,str)
0127     %
0128     if dispmode,
0129         disp(str)
0130     end
0131 end

Community support and wiki are available on Redmine. Last update: 18-Apr-2019.