How do I add space between strings when I am using randperm?

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 件のコメント

Walter Roberson
Walter Roberson 2013 年 10 月 2 日
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.
Elisa
Elisa 2013 年 10 月 2 日
thanks

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

 採用された回答

Walter Roberson
Walter Roberson 2013 年 10 月 2 日

1 投票

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 件のコメント

Elisa
Elisa 2013 年 10 月 2 日
Thanks a lot walter,
However, unfortunately, it gives me something like that:
BCDFG HTGRW
While I would like:
G T H J K
Thanks again, anyway!!!
Walter Roberson
Walter Roberson 2013 年 10 月 2 日
Your question asked for a space between strings, not for a space between characters.
set_string1 = string( randperm(21,set_length) );
reshape([set_string1; blanks(set_length)], 1, [])
Elisa
Elisa 2013 年 10 月 3 日
Thanks a lot walter!!!
It works actually, however, strangely, it does it just on the 'command window' not on the 'static test' of the GUI I'm programming. I don't know why(!?).
thanks a lot anyway
Walter Roberson
Walter Roberson 2013 年 10 月 3 日
Please show how you create the string and put it into the static text.
Elisa
Elisa 2013 年 10 月 3 日
thanks a lot Walter. Now, I've solved it!
I needed to define that your code is in fact my variable:
string = 'BCDFGHJKLMNPQRSTVWXYZ';
numRands = length(string);
set_length = 5;
set_string = string( randperm(21,set_length)); %21 consonants
set_string = reshape([set_string; blanks(set_length)], 1, []);
set(handles.show_set_string_inStaticTextInGUI,'String',set_string);
Thanks a lot!!!!!!

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

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2013 年 10 月 3 日

0 投票

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 件のコメント

Elisa
Elisa 2013 年 10 月 3 日
Thanks a lot Sean, but this give me error
Undefined function 'strjoin' for input arguments of type 'cell'.
Anyway, I solved it with Walter method.
Thanks a lot anyway!!! I've learned a lot of new things :)
Sean de Wolski
Sean de Wolski 2013 年 10 月 4 日
strjoin is new in 13a I believe.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by