Need help plotting 100 Strings onto grid (FOR loop not working)!

1 次查看(过去 30 天)
MATLAB has been working on this code for over 30 minutes and still hasnt processed it:
%
hold on
axis on
for i = 1:100
for j = 1:100
annotation('textbox', [i/100 j/100 0.01 0.01], 'String', image(i,j));
end
end
Image() is a matrix 100x100 which contains a number at each position.
I read online that matlab isnt very efficient at for loops, and since i am iterating 10,000 times, that explains it.
I dont know how to vectorise this code either. PCOLOR, SURF, MESH etc dont deal with plotting "Strings".
Would appreciate your help,

回答(3 个)

Walter Roberson
Walter Roberson 2013-4-22
[I, J] = ndgrid(1:100, 1:100);
strs = cellstr(num2str(imagedata(:)));
text( J(:)./100, I(:)./100, strs );
Note the change of variable name -- "image" is the name of an important MATLAB graphics function.
Please note the exchange of I and J. This is needed if what you are doing is labeling blocks that you have displayed using image() or imagesc() or imshow(). For example,
data = floor(rand(3,5) * 100);
strs = cellstr(num2str(data(:)));
[I,J] = ndgrid(1:size(data,1),1:size(data,2));
imagesc(data);
t = text(J(:)./1, I(:)./1, strs); %in this example coordinates are integer

Matt Kindig
Matt Kindig 2013-4-22
I would use a text() function rather than a textbox annotation, since the former is a lot lighter than the latter.
You can then call it without using a loop at all by calling it as:
[ii,jj]=meshgrid( 1:size(image,1)/100, 1:size(image,2)/100); %get x,y values
ii = reshape(ii, [], 1);
jj = reshape(jj, [], 1);
img = reshape(image, [], 1);
text(ii, jj, num2str(img));

Kelly Kearney
Kelly Kearney 2013-4-22
The slowdown is probably because you're trying to create 10000 annotation objects, and Matlab tends to get bogged down with too many graphics objects. You can achieve the numbers in a grid effect a lot more efficiently using the text command along with appropriate grid lines:
n = 100;
im = round(rand(n)*10);
xgrid = linspace(0,1,n+1);
xcent = (xgrid(1:end-1)+xgrid(2:end))./2;
[x,y] = meshgrid(xcent);
axes('position', [0 0 1 1], 'xlim', [0 1], 'ylim', [0 1], ...
'xtick', xgrid, 'ytick', xgrid, 'xgrid', 'on', ...
'ygrid', 'on', 'gridlinestyle', '-');
text(x(:), y(:), cellstr(num2str(im(:))), 'horiz', 'center');
A few side notes:
- Using image as the name of your array is a bad idea, since that will overshadow the image function.
- Matlab is pretty good at loops these days; most of the comments relating to slow iteration over loops refers to older versions.
- Are you sure you want to display a 100x100 grid of numbers? Even with my single-digit example above, that gets a bit crowded even for a maximized figure on a decent-sized monitor.
  2 个评论
Delvin
Delvin 2013-4-22
Thanks thats the perfect solution, except it renders it upside. Any way i can flip it upside down?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by