How does one produce an array of strings automatically?

4 visualizaciones (últimos 30 días)
Oliver
Oliver el 14 de Abr. de 2014
Comentada: Walter Roberson el 16 de Abr. de 2014
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

Respuesta aceptada

Mischa Kim
Mischa Kim el 14 de Abr. de 2014
Editada: Mischa Kim el 14 de Abr. de 2014
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 comentarios
Sean de Wolski
Sean de Wolski el 16 de Abr. de 2014
Perhaps you want:
T = vertcat(totalVec{:})
Oliver
Oliver el 16 de Abr. de 2014
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.

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 14 de Abr. de 2014
There is an undocumented but extremely efficient function for this job:
stringVector = sprintfc('v%d', 1:5)

Sean de Wolski
Sean de Wolski el 16 de Abr. de 2014
And a documented one liner:
x = strcat('v',cellstr(num2str((1:5).')))

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by