Unable to perform assignment because the indices on the left side are not compatible with the size of the right side

1 view (last 30 days)
I am confused yet . I hoped to get string (i dont know what i have to say character or string) values for every matrix elements but i didnt happen.
Example- Input=[1.25 2.2;3.5 6.2] and Output=['1.25' '2.2';'3.5' '6.2]
Can anyone explain me please, whats the error inside the code ?
%Code
function y=elementsnumber2string(x)
[m,n]=size(x);
y=[];
for i=1:m
for j=1:n
y(i,j)=num2str(x(i,j));
end
end
end
%Output
Unable to perform assignment because
the indices on the left side are not
compatible with the size of the right
side.
Error in findafterdecimalponts (line
6)
y(i,j)=num2str(x(i,j));

Answers (1)

Steven Lord
Steven Lord on 1 May 2020
y(i, j) refers to exactly one element of y as you've defined i and j. How many elements does, say, '10' have?
numel(num2str(10))
You can't fit two characters into a space big enough to hold one.
Depending on which release you're using and how you want to use y, what you're trying to do could be as simple as calling string on the matrix x.
x = magic(5)
y = string(x)
y(2, 4)
  1 Comment
Syed Shahed
Syed Shahed on 1 May 2020
Edited: Syed Shahed on 1 May 2020
Thank you for replying . But i want single quotes rather than double ones like '1' like character.
I want to do the operation like this along a whole matrix .
x=2.2
num2string(x)
ans= '2.2'
Can i do that? is it possible ?
Actually i want to convert the x=[1.25 2.2;3.5 6.2] matrix into a character matrix (array) like y=['1.25' '2.2';'3.5' '6.2]. Then i want to define the max decimal points that the character has among all the character.
How would i do that ? Any idea please

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!