Filling array with dec2hex() value

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
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);
@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 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by