From: CLou on
Hi,

I'm trying to concat a string to each string in a cell array of strings using:

first = 'first_'
attach = { 'end' 'end1' 'end31' }
i=1:1:size(attach,2)
cellfun( @(x) [first x(i) ], attach, 'UniformOutput',false)

It works, except that all of the values in attach are truncated to 'end'. How do I prevent that from happening?

Thanks!

Cathy
From: Andy on
"CLou " <cathy(a)prismstoneconsultingTO.com> wrote in message <i0g266$s6s$1(a)fred.mathworks.com>...
> Hi,
>
> I'm trying to concat a string to each string in a cell array of strings using:
>
> first = 'first_'
> attach = { 'end' 'end1' 'end31' }
> i=1:1:size(attach,2)
> cellfun( @(x) [first x(i) ], attach, 'UniformOutput',false)
>
> It works, except that all of the values in attach are truncated to 'end'. How do I prevent that from happening?
>
> Thanks!
>
> Cathy

% quick fix

first = 'first_';
attach = { 'end' 'end1' 'end31' };
cellfun( @(x) [first x ], attach, 'UniformOutput',false)
From: Andy on
Oh, I suppose I should explain the problem as well. As you had written it, i=1:3. Cellfun already applies your function to each element in the array. So the function you were applying was (pseudo-code):

[first 'end'(1:3)];
[first 'end1'(1:3)];
[first 'end31'(1:3)];

In this case, the initial substring of length three was always 'end'. I hope that's clear.
From: Jos (10584) on
"CLou " <cathy(a)prismstoneconsultingTO.com> wrote in message <i0g266$s6s$1(a)fred.mathworks.com>...
> Hi,
>
> I'm trying to concat a string to each string in a cell array of strings using:
>
> first = 'first_'
> attach = { 'end' 'end1' 'end31' }
> i=1:1:size(attach,2)
> cellfun( @(x) [first x(i) ], attach, 'UniformOutput',false)
>
> It works, except that all of the values in attach are truncated to 'end'. How do I prevent that from happening?
>
> Thanks!
>
> Cathy

Why do it the hard way? ML offers some excellent string functions, like STRCAT:

first = 'first_'
attach = { 'end' 'end1' 'end31' }
result = strcat(first,attach)

hth
Jos
From: CLou on
"Andy " <theorigamist(a)gmail.com> wrote in message <i0g2ud$ibi$1(a)fred.mathworks.com>...
> "CLou " <cathy(a)prismstoneconsultingTO.com> wrote in message <i0g266$s6s$1(a)fred.mathworks.com>...
> > Hi,
> >
> > I'm trying to concat a string to each string in a cell array of strings using:
> >
> > first = 'first_'
> > attach = { 'end' 'end1' 'end31' }
> > i=1:1:size(attach,2)
> > cellfun( @(x) [first x(i) ], attach, 'UniformOutput',false)
> >
> > It works, except that all of the values in attach are truncated to 'end'. How do I prevent that from happening?
> >
> > Thanks!
> >
> > Cathy
>
> % quick fix
>
> first = 'first_';
> attach = { 'end' 'end1' 'end31' };
> cellfun( @(x) [first x ], attach, 'UniformOutput',false)

Wow...that was fast. Looks like I outsmarted myself :) Thanks a bunch!