How to split each element in a cell array into four equal parts?

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 comentarios

I recommend providing a small example of input and its desired output.
Thank you, that’s a good idea. I will update it with examples now!

Iniciar sesión para comentar.

 Respuesta aceptada

Use curly braces {} to access the vectors in the individual cells of splitBinaryUserMessage:
% userMessage = input('Please input your message here: ');
userMessage = 'random';
% 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);
{'01110010'} {'01100001'} {'01101110'} {'01100100'} {'01101111'} {'01101101'}
% Split each 8 bit number into four parts, two bits in each part
for i = 1:length(splitBinaryUserMessage)
part1{i} = splitBinaryUserMessage{i}(1:2);
part2{i} = splitBinaryUserMessage{i}(3:4);
part3{i} = splitBinaryUserMessage{i}(5:6);
part4{i} = splitBinaryUserMessage{i}(7:8);
end
disp(part1); disp(part2); disp(part3); disp(part4);
{'01'} {'01'} {'01'} {'01'} {'01'} {'01'} {'11'} {'10'} {'10'} {'10'} {'10'} {'10'} {'00'} {'00'} {'11'} {'01'} {'11'} {'11'} {'10'} {'01'} {'10'} {'00'} {'11'} {'01'}

4 comentarios

Thank you for responding so quickly!
I just tried this and although it now displays them correctly (two bits per element), it displays them in an odd way. They're not just displayed in a list, each line of elements get increasingly longer. I'm not sure if this is right?
For example:
{'01'}
{'01'}
{'01'} {'00'}
{'01'} {'00'} {'01'}
{'01'} {'00'} {'01'} {'01'}
{'01'} {'00'} {'01'} {'01'} {'01'}
{'01'} {'00'} {'01'} {'01'} {'01'} {'00'}
{'01'} {'00'} {'01'} {'01'} {'01'} {'00'} {'01'} {'01'}
{'00'} {'00'} {'00'} {'00'} {'01'} {'00'} {'00'} {'00'} {'01'}
{'01'} {'00'} {'01'} {'01'} {'01'} {'00'} {'01'} {'01'} {'01'} {'00'}
... and so on.
Then it next displays this:
Columns 1 through 12
{'01'} {'00'} {'01'} {'01'} {'01'} {'00'} {'01'} {'01'} {'01'} {'00'} {'01'} {'01'}
Column 13
{'00'}
Columns 1 through 12
{'00'} {'10'} {'10'} {'10'} {'11'} {'10'} {'11'} {'10'} {'11'} {'10'} {'10'} {'10'}
Although the lengths vary.
Please, why is Matlab displaying them in these structures?
Because the disp is inside the for loop (or semicolon(s) are missing at the end(s) of lines inside the for loop). In other words, it is showing how the part1, etc., are built with each iteration of the for loop, whereas my answer just displays them at the end, after the for loop is done.
Ah of course!
Thank you so much for all your help :)
You're welcome! Let me know if you have any other questions about this. Otherwise, please click "Accept this Answer" to mark it as solved. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Abr. de 2022

Comentada:

el 11 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by