Insert string into cell array beside data

9 次查看(过去 30 天)
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)...

回答(1 个)

dpb
dpb 2016-12-15
编辑:dpb 2016-12-16
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.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by