How to split each element in a cell array into four equal parts?
Show older comments
Hello!
In my program, I ask the user to input a string. I then convert each character of that string into 8 bit numbers, which I store individually as elements in a cell array.
I would then like to split each of those elements into 4 equal parts, meaning that each 8 bit number is now split into 4 parts containing 2 bits each.
But I don't know how to do this and loop through each one.
Here's my code so far (the loop doesn't work):
% Ask user to input message
userMessage = input('Please input your message here: ');
% Take individual character and convert to 8 bit binary number
% Do this for each character in the userMessage
userMsgLength = strlength(userMessage);
binaryUserMessage = dec2bin(double(userMessage), 8);
% Isolate each 8 bit number for each character
splitBinaryUserMessage = cellstr(reshape(binaryUserMessage,[],8));
disp(splitBinaryUserMessage);
% Split each 8 bit number into four parts, two bits in each part
for i = 1:length(splitBinaryUserMessage)
part1{i} = splitBinaryUserMessage(1:2);
part2{i} = splitBinaryUserMessage(3:4);
part3{i} = splitBinaryUserMessage(5:6);
part4{i} = splitBinaryUserMessage(7:8);
disp(part1);
disp(part2);
disp(part3);
disp(part4);
end
Please can someone help? Thank you!
2 Comments
Matt J
on 11 Apr 2022
I recommend providing a small example of input and its desired output.
Eleanor Stevens
on 11 Apr 2022
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!