Please help me with the code....

4 views (last 30 days)
radhika s gowda
radhika s gowda on 7 Feb 2016
Edited: per isakson on 10 Feb 2016
How to add space between each characters in matrix and those should be converted into ASCII...
For example taking string as input ie, matlabiseasy that has to be stored in matrix and remaining should be replaced with zeros as
m a t l
a b i s
e a s y
0 0 0 0
But when I execute this... there won't be space between 2 characters. ie,
matl
abis
easy
0000
As in C dont we have any fuction as /t for space... And how to convert the character to ASCII please help me with this code..
  1 Comment
per isakson
per isakson on 7 Feb 2016
Edited: per isakson on 7 Feb 2016
Now I realize that &nbsp"..."&nbsp in&nbsp "execute this... there" &nbspis a reference to your previous question.

Sign in to comment.

Answers (2)

per isakson
per isakson on 7 Feb 2016
Edited: per isakson on 9 Feb 2016
A start
>> str = sprintf( '%c ', 'matlabiseasy' )
str =
m a t l a b i s e a s y
>> str2 = permute( reshape( str, 8, [] ), [2,1] )
str2 =
m a t l
a b i s
e a s y
>> double( str2 )
ans =
109 32 97 32 116 32 108 32
97 32 98 32 105 32 115 32
101 32 97 32 115 32 121 32
Combining this with Walter's answer to your previous question
YourString = 'matlabiseasy_matlabiseasy_';
L = length( YourString );
N = ceil(sqrt(L));
A = char( '0' .* ones(N,N) );
A(1:L) = YourString;
str = sprintf( '%c ', A );
str = reshape( str, [2*N,N] );
permute( str, [2,1] )
outputs
ans =
m a t l a b
i s e a s y
_ m a t l a
b i s e a s
y _ 0 0 0 0
0 0 0 0 0 0
In response to comment "Let me explain ..."
YourString = 'matlabiseasy_1234567890_abcdefghijklmnopqrstuvwxyz';
L = length( YourString );
e = ceil( log2(sqrt(L)) - log2(4) );
N = 4*2^e;
A = char( '0' .* ones(N,N) );
A(1:L) = YourString;
A = permute( A, [2,1] );
Nc = 4.*ones( 1, 2^e );
Rc = mat2cell( A, Nc, Nc )
r11 = Rc{1,1}
outputs
Rc =
[4x4 char] [4x4 char]
[4x4 char] [4x4 char]
r11 =
matl
easy
4567
abcd
  10 Comments
Walter Roberson
Walter Roberson on 8 Feb 2016
The version that runs all of the letters together on one line is the solution. In MATLAB, a string is the same thing as a row of characters, so if you display a character array then it is going to display as strings. That does not mean that everything in the row has been stored in the first element of the row.
If you really want to the output could instead be made a cell array of strings, one character oer cell. That would make it easier to see that there is only one character stored in each position, but it does make it more difficult to work with the characters afterwards.
per isakson
per isakson on 8 Feb 2016
Edited: per isakson on 10 Feb 2016
  • "... input as string ... to be stored in 4x4 8x8 or 16x16 [matrix] ... [padded] with zeros"
  • "4x4, 8x8 or 16x16" &nbsp thus I assume that 12x12 isn't allowed.
  • "... need matrix to be divided into 4 equal parts ..." &nbsp I understand that the size of the resulting matrix shall be &nbsp<4*2^k x 4*2^k>&nbsp where k is a whole number.
  • "equal parts" &nbsp are <4x4> character arrays
  • You don't mention the "delimiter", tab or space, in your comment.
I propose
  • See Fundamental MATLAB Classes and decide what "data type" you want for the output
  • Store the result as a square cell array of&nbsp<4x4> character arrays. See answer above.

Sign in to comment.


Image Analyst
Image Analyst on 8 Feb 2016
I think this will do what you want:
str = 'matlabiseasy'
len = length(str);
fprintf('Original length of str = %d\n', len);
% Compute how many rows.
numRows = ceil(sqrt(len))
% However it's not a multiple of 4 yet.
% Make it a multiple of 4
numRows = ceil(numRows / 4) * 4
% Add zeros
str = [str, zeros(1, numRows^2-len)+'0']
fprintf('Now length of str = %d\n', length(str));
% Reshape into a square array
out = reshape(str', numRows, numRows)'
I've tested it with strings of different lengths, and the output is always a square matrix where the number of rows and columns is a multiple of 4 like you wanted. Note that it may not look "square" when printed out in the command window but it is. When echoing to the command window, there is less room between characters on the same line, than between different lines, so it will look taller than wide, but it is still square regardless of how it looks. If you really want spaces, you can do that with fprintf() rather than just let MATLAB decide on it's own how to display it.
Now, can you tell us why you wanted this funny array?
  2 Comments
Walter Roberson
Walter Roberson on 9 Feb 2016
12 is not allowed as a length, only powers of 2.
The poster is working on an assignment.
And I think what the poster wants to see is the equivalent of
num2cell(out)
I wouldn't recommend that for computation, but it is an option.
Image Analyst
Image Analyst on 9 Feb 2016
OK, right. It would be easy to modify
numRows = ceil(numRows / 4) * 4
to make it be a power of 2 instead of a multiple of 4. I didn't see the poster say it's a homework problem and they didn't tag it as homework, but since it may be an assignment, I'll leave that modification up to them.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!