How can I make a string array consisting of strings with numerical suffixes?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I want to make a 50x1 array that consists of 'sub01', ‘sub02’, ‘sub03’, ‘sub04’, …, ‘sub50’.
And this is my code, but it doesn't work
Sub=[];
for i=1:50
Sub(i,1) = str2num("sub%2d",i);
end
Please help me ☹️
0 comentarios
Respuestas (2)
Cris LaPierre
el 23 de Nov. de 2022
Editada: Cris LaPierre
el 23 de Nov. de 2022
str2num won't work with a word ("sub"). It's also unnecessary if you are using sprintf.
One other correction I would make is in how you preallocate your array. The empty brackets initializes your variable Sub as a double, so all strings will appear as NaN. You need to initiallize as an empty string.
Try something like this.
Sub = strings;
for i=1:50
Sub(i,1) = sprintf("sub%02d",i);
end
% view result
Sub
2 comentarios
Cris LaPierre
el 23 de Nov. de 2022
Not quite the same result, but if you don't care about the leading 0, here's another way to do this.
Sub = "sub" + (1:50)'
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!