MATLAB: Array indexing-show wrong outputs

arraycell arrayscell coloringindexing

Hi I have this part of code
[row,col] = GrubbTest(CD_data_fem(:,2:end))
X=cell2mat(CD_data_fem(:,2:end))
XX = reshape(strtrim(cellstr(num2str(X(:)))), size(X));
XX(row,col) = strcat('<html><body bgcolor="FF5500" width="50px">', XX(row,col));
set(handles.uitable1,'Data',[PN_Female XX]);
row and col variable give me :
row =
25
25
25
19
25
col =
4
6
8
9
9
It color good cells but with some extra(wrong),especialy row 19 col 4,6 and 8
Any ideas? thank you Danny

Best Answer

Note: There are way more powerful formatting functions than num2str and strcat, such as sprintf, the undocumented sprintfc and since R2016b, the mighty compose. Any of these would be better than what you have.
Anyway, your problem is that you're indexing with two vectors. This doesn't work the way you think. The indexed coordinates is the cartesian product of the two vectors, not the memberwise pairs. (i.e. the coordinates are {[row(1), col(1)], [row(1), col(2)], [row(1), col(3)], row(1), col(4)], [row(2), col(1)], ...}.
The fix:
XX(sub2ind(size(XX), row, col)) = ...