I would like to save each byte of a uint_32 number to the elements of a 1x4 matrix using Matlab
a = zeros(1,4,'uint8');
val = uint32(2^32-1);
How can I split val byte-wise and insert each single element in the array?
You may use the typecast function, which converts between data types without changing the underlying data:
a = typecast(uint32(2^32-1), 'uint8')
% produces the array [255 255 255 255]
From what I could understood, I think you are looking for something like this with the least significant byte going as the rightmost element in the output vector -
bits = reshape(bitget(num,32:-1:1),8,[]); %// num is the input number
weights2 = 2.^([7:-1:0]);
out = weights2*bits
bits = reshape(bitget(uint32(1234),32:-1:1),8,[]); matlab will return: Error using * MTIMES is not fully supported for integer classes. At least one input must be scalar. To compute elementwise TIMES, use TIMES (.*) instead. How to solvenum has to be in double format. So, you need to convert that to double before using it. Thus, if you have num = uint32(1234), do this - bits = reshape(bitget(double(num),32:-1:1),8,[]); . Hope this works out and make sense to you!Error using bitget Double inputs must have integer values in the range of ASSUMEDTYPE. bits = reshape(bitget(double(get(pidKpSlider,'Value')*1000),32:-1:1),8,[]);round : ...double(round(get(pid..)?
uint32(2^32)is zero.