How does one produce an array of strings automatically?

I wish to produce an array/vector of strings to use as InputNames and OutputNames on a dynamical system model in something like the following manner (note that the below doesn't work, but illustrates what I'm looking for):
n = 5;
stringVector = {'v' num2str(n)};
in order to produce a vector of strings as such:
stringVector = {'v1', 'v2', 'v3', 'v4', 'v5'}
How can this be done?
Thanks, Olie

 採用された回答

Mischa Kim
Mischa Kim 2014 年 4 月 14 日
編集済み: Mischa Kim 2014 年 4 月 14 日

1 投票

Oliver, you could do
n = 5;
for ii = 1:n
stringVector{ii} = strcat('v',int2str(ii));
end
or simply
stringVector = strcat({'v'},int2str((1:5)'));

3 件のコメント

Oliver
Oliver 2014 年 4 月 16 日
Thank you both, these both look like good approaches - I then also need to take multiple vectors of strings and concatenate them - but this gives me a cell type (not sure if this is correct).
So something like:
for i = 1:5;
vec1{i} = strcat('v1',num2str(i));
end
for i = 1:5;
vec2{i} = strcat('v2',num2str(i));
end
totalVec = {vec1, vec2}
just to result in one vector, totalVec, which is just a list of all the elements of vec1 and vec2.
When I do this it gives something like:
totalVec =
{1x5 cell} {1x5 cell}
Is this correct? Will it just act like a normal string vector?
Thanks.
Sean de Wolski
Sean de Wolski 2014 年 4 月 16 日
Perhaps you want:
T = vertcat(totalVec{:})
Oliver
Oliver 2014 年 4 月 16 日
Oh - I've just discovered, given two vectors of strings, vec1, and vec2, you can produce a concatenation of these vectors with
totalVec = [vec1,vec2]
Thanks for all the help.

サインインしてコメントする。

その他の回答 (2 件)

Jan
Jan 2014 年 4 月 14 日

3 投票

There is an undocumented but extremely efficient function for this job:
stringVector = sprintfc('v%d', 1:5)
Sean de Wolski
Sean de Wolski 2014 年 4 月 16 日

0 投票

And a documented one liner:
x = strcat('v',cellstr(num2str((1:5).')))

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

質問済み:

2014 年 4 月 14 日

コメント済み:

2014 年 4 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by