Using imagesc and text: the highest number does not label the text in a very specific dataset.

6 views (last 30 days)
A weird bug in imagesc and text: the highest number does not show text in R2016a in a very specific dataset:
% the following code shows a matrix using parula, but the label text failed on 18.6003.
figure;
Matrix_grab = [
0.9504 1.0222 1.0292 1.9443 1.9557 1.8992 0.0616 0.0532 0.0535 0.0561 0.0759 0.0597
0.9726 0.9965 1.0316 1.8694 1.8935 1.9168 0.1520 0.1437 0.1683 0.1559 0.1967 0.1157
0.9977 0.9942 1.0081 0.2773 0.2948 0.2843 2.4439 2.3748 2.4214 1.2972 1.6942 1.2437
0.9615 1.0269 1.0127 1.0912 0.9756 0.9279 1.8351 1.7709 1.8954 1.9752 2.6708 1.9203
0.9681 1.0341 0.9988 1.0252 1.3144 1.3775 16.5169 16.6159 17.2979 16.7028 18.6003 15.6852
0.9681 1.0413 0.9919 0.9434 0.9555 0.9673 0.8212 0.7394 0.8193 0.8598 1.0120 0.7378
0.9839 0.9670 1.0509 0.5780 0.5695 0.6856 0.7896 0.9578 0.8385 0.7985 1.1177 0.8093
1.0281 0.9559 1.0174 0.5258 0.5363 0.5172 2.0035 1.7668 1.9175 1.1086 1.4886 1.0629
1.0000 1.0035 0.9965 0.5794 0.5992 0.5859 1.1587 1.1260 1.1089 0.9194 1.1518 0.8574
0.9704 1.0509 0.9805 0.5282 0.5313 0.5089 1.8780 1.8893 1.8351 1.0248 1.2839 0.9758
0.9977 1.0222 0.9805 1.0787 1.1002 1.0908 2.0838 1.9290 1.9805 2.4374 3.0750 2.3370
0.9438 1.0509 1.0081 1.0064 1.0265 1.0319 1.6013 1.4518 1.5539 1.9798 2.0429 1.7227
1.0116 1.0222 0.9670 0.9926 0.9380 0.9109 1.2476 1.2638 1.2362 1.2617 1.6028 1.2351
0.9726 1.0246 1.0034 0.7698 0.8128 0.7468 1.2248 1.0952 1.1885 1.2823 1.6632 1.2553];
imagesc(Matrix_grab);
[X,Y] = meshgrid(1:size(Matrix_grab,2),1:size(Matrix_grab,1));
text(X(:),Y(:),Matrix_grab(:),num2str(round(Matrix_grab(:),2)),'HorizontalAlignment','center','VerticalAlignment','middle');
% if I change the 18.6003 to 17.0000, now the 17.2979 does not show.
figure;
Matrix_grab = [
0.9504 1.0222 1.0292 1.9443 1.9557 1.8992 0.0616 0.0532 0.0535 0.0561 0.0759 0.0597
0.9726 0.9965 1.0316 1.8694 1.8935 1.9168 0.1520 0.1437 0.1683 0.1559 0.1967 0.1157
0.9977 0.9942 1.0081 0.2773 0.2948 0.2843 2.4439 2.3748 2.4214 1.2972 1.6942 1.2437
0.9615 1.0269 1.0127 1.0912 0.9756 0.9279 1.8351 1.7709 1.8954 1.9752 2.6708 1.9203
0.9681 1.0341 0.9988 1.0252 1.3144 1.3775 16.5169 16.6159 17.2979 16.7028 17.0000 15.6852
0.9681 1.0413 0.9919 0.9434 0.9555 0.9673 0.8212 0.7394 0.8193 0.8598 1.0120 0.7378
0.9839 0.9670 1.0509 0.5780 0.5695 0.6856 0.7896 0.9578 0.8385 0.7985 1.1177 0.8093
1.0281 0.9559 1.0174 0.5258 0.5363 0.5172 2.0035 1.7668 1.9175 1.1086 1.4886 1.0629
1.0000 1.0035 0.9965 0.5794 0.5992 0.5859 1.1587 1.1260 1.1089 0.9194 1.1518 0.8574
0.9704 1.0509 0.9805 0.5282 0.5313 0.5089 1.8780 1.8893 1.8351 1.0248 1.2839 0.9758
0.9977 1.0222 0.9805 1.0787 1.1002 1.0908 2.0838 1.9290 1.9805 2.4374 3.0750 2.3370
0.9438 1.0509 1.0081 1.0064 1.0265 1.0319 1.6013 1.4518 1.5539 1.9798 2.0429 1.7227
1.0116 1.0222 0.9670 0.9926 0.9380 0.9109 1.2476 1.2638 1.2362 1.2617 1.6028 1.2351
0.9726 1.0246 1.0034 0.7698 0.8128 0.7468 1.2248 1.0952 1.1885 1.2823 1.6632 1.2553];
imagesc(Matrix_grab);
[X,Y] = meshgrid(1:size(Matrix_grab,2),1:size(Matrix_grab,1));
text(X(:),Y(:),Matrix_grab(:),num2str(round(Matrix_grab(:),2)),'HorizontalAlignment','center','VerticalAlignment','middle');
% but 99.9999% of the other datasets are OK, for example:
figure;
Matrix_grab = [
0.94815 1.1194 0.94088 0.80002 0.77555 0.81092 0.051856 0.043311 0.045419 0.048842 0.039836 0.046536
0.95696 1.0325 1.0107 0.8476 0.76665 0.82987 0.13717 0.1186 0.14688 0.15047 0.10537 0.12056
1.0304 0.96111 1.0084 1.7149 1.651 1.7262 1.9597 1.6712 1.9581 2.9778 2.9286 2.917
1.028 0.98585 0.98538 0.94264 0.93301 0.93582 1.6904 1.4315 1.5649 1.7912 1.7616 1.8676
0.96808 0.93483 1.1035 0.64088 0.55221 0.80532 13.5542 11.8014 13.8271 15.3936 15.2444 15.5031
1.0717 0.9589 0.97181 1.3239 1.3013 1.2962 0.78858 0.59773 0.66255 0.99373 0.99094 1.0506
1.0139 0.99271 0.99223 0.96245 1.021 1.1442 0.76172 0.70591 0.73514 0.75311 0.73046 0.76905
1.0304 0.99501 0.97406 0.85745 0.87861 0.89356 1.9062 1.5701 1.7525 0.78147 0.80676 0.82616
1.0162 0.96111 1.0225 0.98495 0.94822 0.98462 1.0215 0.91439 1.0566 1.1129 1.1021 1.1053
1.0791 0.9589 0.9651 0.96914 0.96591 0.96213 1.6787 1.4315 1.7125 0.46682 0.46229 0.47014
0.98159 0.98813 1.0296 0.90634 0.86053 0.89977 2.0009 1.5811 1.8914 2.2103 2.1588 2.2571
0.92864 1.0664 1.0084 1.1081 1.0521 1.131 1.5483 1.2319 1.4737 1.5202 1.173 1.3798
1.0045 1.0042 0.98994 0.96691 0.95042 0.94015 1.2008 1.0528 1.1509 1.4516 1.2955 1.4619
0.98614 0.99962 1.0131 0.9492 0.9395 0.94233 1.0923 0.89351 1.0042 1.0006 1.0118 1.0506];
imagesc(Matrix_grab);
[X,Y] = meshgrid(1:size(Matrix_grab,2),1:size(Matrix_grab,1));
text(X(:),Y(:),Matrix_grab(:),num2str(round(Matrix_grab(:),2)),'HorizontalAlignment','center','VerticalAlignment','middle');
  1 Comment
raym
raym on 18 Mar 2025
Edited: raym on 18 Mar 2025
OMG! I just realized that It's my fault that the 3rd argument should be removed, and use the following code instead. It's very lucky that I have been using the wrong code for many times with no problem.
text(X(:),Y(:),arrayfun(@num2str,round(Matrix_grab(:),2),'uni',0),'HorizontalAlignment','center','VerticalAlignment','middle');

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!