Clear Filters
Clear Filters

Insert string into cell array beside data

9 views (last 30 days)
HEEKWON LEE
HEEKWON LEE on 15 Dec 2016
Edited: dpb on 16 Dec 2016
I would like to insert string for indexing beside data currently i have just data [1;2;4;.....] but it has special position as own has so if i want to sort those easily, had better insert specific string in there. It has some rules, "R" 10times, then "C" 10times finally "L" also ten times... and repeat and repeat......
How can I insert specific repeated string(char)...

Answers (1)

dpb
dpb on 15 Dec 2016
Edited: dpb on 16 Dec 2016
You can build character string sequences by memory manipulation just the same as numeric ones:
>> c='RCL'; % the initial characters to replicate
>> N=10; % the replication count for each
>> c=reshape(repmat(c.',1,N).',1,[]) % a pattern of N of the substring
c =
RRRRRRRRRRCCCCCCCCCCLLLLLLLLLL
>>
Now just replicate that as needed and concatenate with the rest into a cell array.
M=3; % say, whatever is multiplier need for final length
c=repmat(c.',M,1);
ADDENDUM
For brevity of presentation I purposely left the above as a row vector; the conversion to cellstr array should be obvious but just to make sure...
>> c=cellstr(reshape(repmat(c.',1,N).',[],1));
>> whos c
Name Size Bytes Class Attributes
c 30x1 1860 cell
>>
Is the column cellstring array needed... NB: reordered the reshape to '[],1' so cellstr is working on a column character string array.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!