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,
0 件のコメント
回答 (3 件)
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
0 件のコメント
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));
0 件のコメント
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 件のコメント
Walter Roberson
2013 年 4 月 22 日
Try
axis image
and if that is not satisfactory,
axis xy
and if not that then
axis ij
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!