mHelpMe
mHelpMe

Reputation: 6668

adding a number to a cell string

I am trying to append a number to a cell string ('fract') the code is below. After executing the code though all the cells in fract_lbl are still just 'fract'.

fract_lbl = repmat({'fract'}, int_fract, 1);
num_vec = [1:int_fract]';
fract_lbl = strcat(fract_lbl, num2cell(num_vec));

How can I append a number to a cell string?

Upvotes: 0

Views: 224

Answers (2)

Sardar Usama
Sardar Usama

Reputation: 19689

With ≥ R2016b, you can skip repmat and concatenate strings directly with plus as follows:

Str = 'fract' + string(numvec);

Implicit expansion is happening in above line.

The above line gives a string array. In your question, you actually have a cell array containing characters in each cell, not strings. If you need a cell array of characters then you can further use cellstr as follows:

Str = cellstr(Str);

Upvotes: 1

Paolo
Paolo

Reputation: 26084

You should use a function such as compose, and use the format specifiers:

fract_new = compose('%s%d',fract_lbl{1}, num_vec)

assuming that the prefix fract remains the same.

Upvotes: 2

Related Questions