How to split this?
Show older comments
I have M as a result of another function.
and I have to split M like C automatically...
I want to split it every 16 bits.
M => input split function => output C
M = '01100001011000100110001110000000000000000000000000000000000000000000000000000000'
????
C = {'0110000101100010'; '0110001110000000'; '0000000000000000'; '0000000000000000';'0000000000000000';}
how to make it? I'm stuck on this problem...
can you help me?
Accepted Answer
More Answers (3)
Image Analyst
on 12 Jun 2020
Did you try a simple for loop:
M = '01100001011000100110001110000000000000000000000000000000000000000000000000000000'
counter = 1;
for index1 = 1 : 16 : length(M)
index2 = min(length(M), index1 + 15);
c{counter} = M(index1 : index2);
counter = counter + 1;
end
celldisp(c)
2 Comments
Image Analyst
on 12 Jun 2020
Note: This robust, general purpose loop works even if M is not a multiple of 16 characters. Otherwise (if it is known to be a multiple of 16) I'd use reshape().
Younghun Kim
on 12 Jun 2020
David Hill
on 12 Jun 2020
You could make a matrix
newMatrix=reshape(M(1:floor(length(M)/16)*16),16,[])';
1 Comment
Younghun Kim
on 12 Jun 2020
Walter Roberson
on 12 Jun 2020
temp = textscan(M,'%16s');
C = temp{1};
This handles M that are not multiples of 16 characters.
Note, though, that if M contains whitespace, that it will not split by 16 character groups, and would instead treat each "word" as the beginning of a section to be split.
1 Comment
Younghun Kim
on 12 Jun 2020
Categories
Find more on Creating and Concatenating Matrices 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!