How do I add space between strings when I am using randperm?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Elisa
el 2 de Oct. de 2013
Comentada: Sean de Wolski
el 4 de Oct. de 2013
Sorry, I'm not expert but I'm learning.
I've tried some answer regarding how to add space between strings, but it seems that these methods do not apply when using a random generation of string.
If I use matrix concatenation (horzcat): ['A', ' ', 'B'] it will generate the space randomly.
if I use STRCAT then it give me error 'index exceed size of matrix' whatever I do.
This is my code:
string = ['BCDFGHKJLMNPQRSTVWXYZ'];
numRands = length(string);
set_length = 5;
set_string = string( round(randperm(21,set_length))); %21 consonants
Thanks a lot in advance for any help you might provide!
2 comentarios
Walter Roberson
el 2 de Oct. de 2013
randperm(21, set_length) is going to produce a list of integers. There is no point in round()'ing the integers before using them as indices.
Respuesta aceptada
Walter Roberson
el 2 de Oct. de 2013
There is a trick to using strcat() to add a space: strcat() will disregard leading or trailing spaces on strings, but will not do so for a cell array of strings. So you can use
set_string1 = string( round(randperm(21,set_length)));
set_string2 = string( round(randperm(21,set_length)));
strcat(set_string1, {' '}, set_string2)
Or, considering that set_string1 and set_string2 are just simple vector of character,
[set_string1, ' ', set_string2]
5 comentarios
Walter Roberson
el 3 de Oct. de 2013
Please show how you create the string and put it into the static text.
Más respuestas (1)
Sean de Wolski
el 3 de Oct. de 2013
You could also do this with strjoin (13a or newer)
string = ['BCDFGHKJLMNPQRSTVWXYZ'];
numRands = length(string);
set_length = 5;
set_string = strjoin(cellstr(string(randperm(21,set_length)).').',' ');
2 comentarios
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!