Sunday 15 February 2015

matlab - Filtering a Cell Array with Recursion -



matlab - Filtering a Cell Array with Recursion -

i'm pretty close on problem. have filter out cell array. cell array can have variety of items in it, want pull out strings, using recursion. pretty close on one. have issue when cells have spaces in them. should get:

test cases: ca1 = {'this' {{{[1:5] true} {' '}} {'is '} false true} 'an example.'}; [filtered1] = stringfilter(ca1) filtered1 => 'this example.' ca2 = {{{{'i told '} 5:25 'her she'} {} [] [] ' knows'} '/take aim , reload'}; [filtered2] = stringfilter(ca2) filtered2 => 'i told knows/take aim , reload'

here have:

%find strings in carr , concatenate them. function [str] = stringfilter(in) str = []; = 1:length(in) %the base of operations case single cell if length(in) == 1 str = ischar(in{:,:}); %if length>1 go through each cell , find strings. else str = stringfilter(in(1:end-1)); if ischar(in{i}) str = [str in{i}]; elseif iscell(in{i}) str1 = stringfilter(in{i}(1:end-1)); str = [str str1]; end end end end

i tried utilize 'ismember', didn't work. suggestions? code outputs following:

filtered1 => 'this example.' filtered2 => '/take aim , reload'

you can quite simplify function to

function [str] = stringfilter(in) str = []; = 1:length(in) if ischar(in{i}) str = [str in{i}]; elseif iscell(in{i}) str1 = stringfilter(in{i}); str = [str str1]; end end end

just loop through elements in cell test, whether string or cell. in latter, phone call function cell again. output:

>> [filtered1] = stringfilter(ca1) filtered1 = example. >> [filtered2] = stringfilter(ca2) filtered2 = told knows/take aim , reload

arrays matlab recursion filtering cell-array

No comments:

Post a Comment