Given that your input value is an integer, you can do the following:
a = mod( floor(instructionWord/2^21), 32)
Another bit solution:
a = bitand( bitshift(instructionWord, -21), hex2dec('1F'))
The last method will cause an error if you feed it with something other than intermediaries.
By the way, your commandWord variable is declared as a signed integer. But if it is an instruction word or something like that, an unsigned integer will make more sense. Expressed expressions expect your input to be only positive. Otherwise, you will need a little more code for modeling >>> (logical shift to the right) in matlab.
source share