Subscripted dimension mismatch error dec2hex

Hello
I searched regarding this error but still could not resolve it. I am using the following code:
function[hexoutput] = hexaoutput(colordata)
[row col] = size(colordata);
hexoutput=zeros(row,col);
for i=1:row
for j=1:col
hexoutput(i,j) = dec2hex(colordata(i,j),6);
end
end
end
I am getting the subscripted dim mismatch error. But this is how we loop through a matrix. I am confused. Any help would be great. Thanks.

 채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 13일

1 개 추천

dec2hex(SOMETHING,6) is going to return 6 (or more) characters. You then try to store those 6 characters into a single numeric array location. Remember, hex is represented as character, but zeros() is numeric, so you are using the wrong datatype for your output array and you are not allowing for the fact that each output needs multiple characters.

댓글 수: 3

Panda
Panda 2016년 4월 13일
편집: Panda 2016년 4월 13일
aha....I get it...what is the way around this then sir? If I want a matrix to be converted to hex values then what changes should I do in my code? The matrix is 122X400...I know its quite large. Could you help here then...? Thanks!
EDIT: ok, I managed to do it. I declared hexoutput as a cell array and then did the same thing. It worked. My another question is - Can't we represent hex in 0X** format?
Consider using sprintf('0x%06x', colordata(i,j))
Panda
Panda 2016년 4월 13일
편집: Panda 2016년 4월 13일
Brother you are simply great!! :) :D Thank you so much!

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

추가 답변 (1개)

Roger Stafford
Roger Stafford 2016년 4월 13일

1 개 추천

The dec2hex(colordata(i,j),6) quantity is a string of at least six hex characters, and yet your are attempting to place it in a single element of hexoutput. It won't fit. You'll have to make hexoutput bigger! Maybe you'd like to make it three-dimensional.

댓글 수: 1

Panda
Panda 2016년 4월 13일
Same ans as above sir...understood my mistake. Thanks

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

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2016년 4월 13일

편집:

2016년 4월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by