Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert hexadecimal values in array in MATLAB

Tags:

matlab

I am very new to matlab. I want to store hexadecimal values in array like this

P=[0x96,0x97,0x98];

But I surfed on google I got no solution for it So first I converted this hexadecimal into decimal so I got array like this

P=[150,151,152];

Now I am trying to get hexadecimal value of values of P array.

I tried

P=[dec2hex(150),dec2hex(151),dec2hex(152)];

But when I am trying to print P(1) then instead of 96 I got only 9. I am not understanding this part. How can I get correct result? Please help me.

like image 864
YLG Avatar asked Feb 11 '23 02:02

YLG


1 Answers

Matlab stores hexadecimal number as character arrays (or strings).

So

a = dec2hex(150)

returns:

a = '96'

concatenating hexadecimal strings as you do:

P=[dec2hex(150),dec2hex(151),dec2hex(152)]

returns:

P = '969798'

Therefore, P(1) = '9'

You probably want to use cell arrays to separately store hex-numbers:

P = {dec2hex(150),dec2hex(151),dec2hex(152)};
P{1}

returns:

P = '96'

to retrieve the numeric value, use

hex2dec(P{1})
like image 81
Nick Avatar answered Feb 15 '23 09:02

Nick