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

extracting a substring from all elements in a cell array?

170 views
Skip to first unread message

Bruce Bowler

unread,
Dec 29, 2011, 10:46:39 AM12/29/11
to
There may be a better way to store my data and I'm open to any and all
suggestions (that help with this problem anyway :-)

I have a cell array of strings. For each element in the array, I'd like
to create a new cell array that contains characters 9:16 of the original
array.

For a single element, I can do xxx = cellarray{1}(9:16) but there doesn't
seem to be a syntax that works for all elements. I know I can do it in a
loop, but that just doesn't feel "right"...

Any ideas?

Thanks!
Bruce

ScottB

unread,
Dec 29, 2011, 11:57:08 AM12/29/11
to
Bruce Bowler <bbo...@bigelow.org> wrote in message <9m3gav...@mid.individual.net>...
c_array(1,:) = {'jhdfsjhdkhfkhgsbltgfdgrsjhgfkf'};
c_array(2,:) = {'hammerheaddogcatkhfldrfgrew'};
c_array(3,:) = {'wolfsprinklefishndfdfutsjfhddkh'};
c_array(4,:) = {'woofbarkdonkeyfontgfddsjghi'};
whos
s_array = char(c_array);
ps_array(:,1:8) = s_array(:,9:16);
pc_array = cellstr(ps_array);
disp(pc_array)
whos

Leslie McBrayer

unread,
Dec 29, 2011, 1:03:42 PM12/29/11
to
"Bruce Bowler" <bbo...@bigelow.org> wrote in message
news:9m3gav...@mid.individual.net...
Another way:

shorter_c = cellfun(@(x) x(9:16), cellarray, 'UniformOutput', false)

Bruce Bowler

unread,
Dec 30, 2011, 3:01:43 PM12/30/11
to
On Thu, 29 Dec 2011 13:03:42 -0500, Leslie McBrayer wrote:

> Another way:
>
> shorter_c = cellfun(@(x) x(9:16), cellarray, 'UniformOutput', false)

Ah... Thanks!

Bruce

0 new messages