Hello,
I have an issue with fill my matrix after the use of dec2hex() function.
As the first step, I allocate space for a three-column matrix. The next step is to generate a random number and delete the decimal part of the number. Which are saved to the first column of matrix. In the last step, I want to converse decimal value to hexadecimal value by dec2hex() function. The conversion to show me basic error: " Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2." Is the problem with the output of the dec2hex() function?
Could you help me with the issue?
%%allocation memory for matrix
for n=1:len
data_extend(n,1) = 0;
data_extend(n,2) = 0;
data_extend(n,3) = 0;
end
%randomize fill of data_extend array
a = 0;
b = 255;
for n=1:len
data_extend(n,1) = (b-a).*rand(1,1) + a; %%rand number from interval 0-255; a-b
data_extend(n,1) = floor(data_extend(n,1)); %%delete decimal
end
%%conversion decimal to hex
for n=1:len
data_extend(n,3) = dec2hex(data_extend(n,1));
end

 채택된 답변

DGM
DGM 2021년 5월 11일
편집: DGM 2021년 5월 11일

1 개 추천

Observe that the output of dec2hex() is a character array:
x = 123
hx = dec2hex(x)
gives
x =
123
hx =
'7B'
So in this example, the input is scalar numeric, but the output is 1x2 char. Not only is the size mismatched, they aren't the same datatype. You'll have to come up with a different way of storing the output -- a cell array or something.

댓글 수: 2

%% allocation memory for matrix
data_extend = zeros(len,3);
Robert Plsicik
Robert Plsicik 2021년 5월 12일
@DGM Thank you for you helpfully answer.
@per isakson I know the method, which you mentioned, however the matlab is not my primary programing language, sometimes I can't think of some matlab functions. Thank you for the reminder.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

태그

질문:

2021년 5월 11일

댓글:

2021년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by