Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[TMW][cell] Deconvoluting (flattening) nested cell array

173 views
Skip to first unread message

Michael Robbins

unread,
Jun 18, 2005, 5:28:39 PM6/18/05
to
Oftentimes my code needs to deal with an unexpectedly convoluted
(nested) cell array. For instance, it expects

s = {'a','b','c'}

but it gets

s = {{'a','b'},'c'}

This happens frequently and I wrote some code to flatten the latter
example to become the former.

Does anybody have well written code for this? Mine's a little
kludgy? I'm assuming I have left out some possible circumstances
when I slapped it together.

Jos

unread,
Jun 18, 2005, 5:49:37 PM6/18/05
to

Michael,
I quickly modified this from celldisp:

% ===========================
function s = mrt(c)
error(nargchk(1,1,nargin));
if ~iscell(c),
error('Must be a cell array.');
end

s{1} = [] ;
for i=1:prod(size(c)),
if iscell(c{i}),
y = mrt(c{i}) ;
[s{end+1:end+length(y)}] = deal(y{:}) ;
else
s{end+1} = c{i} ;
end
end
s(1) = [] ;
% ===========================

hth,
Jos

yun...@gmail.com

unread,
Apr 20, 2015, 2:36:22 PM4/20/15
to
0 new messages