Append a string to a cell array of strings

Hey Guys,
So if I have a function f(string) and I have a cell array i{j} of a set of strings, how do i do something like f('abc' i{j}) such that the arugment passed into f is the concatenation of 'abc' and i{j}

Antworten (2)

Wayne King
Wayne King am 19 Mai 2012

0 Stimmen

x = {'abc', 'def','ghijk'};
y = 'lmnop';
x = char(x);
x = reshape(x,1,size(x,1)*size(x,2));
x = [y x];
indices = isspace(x);
x = x(indices~=1);
Then pass x to your function.
You look like your prepending your string in your post, so that's what I did. You may or may not care about the spaces.

3 Kommentare

Jan
Jan am 19 Mai 2012
Your example creates the string 'lmnopadgbehcfijk'. I'm confused about the purpose of this as I'm confused by the question.
Wayne King
Wayne King am 19 Mai 2012
I was confused by the question as well. I thought the poster was saying that input had to be a string to the function, not a cell array. Doesn't strcat('abc' ,x) create a cell array (where x is the original cell array of strings)
Walter Roberson
Walter Roberson am 20 Mai 2012
I would want to test to be sure, but I think If any input to strcat is a cell array, then the output from strcat is a cell array, even if all cell arrays involved only have a single entry.

Melden Sie sich an, um zu kommentieren.

Jan
Jan am 19 Mai 2012

0 Stimmen

Perhaps you want this:
f(['abc', i{j}])
or
f(strcat('abc', i{j}))

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 19 Mai 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by