Need help plotting 100 Strings onto grid (FOR loop not working)!
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (3)
Walter Roberson
il 22 Apr 2013
[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 Commenti
Matt Kindig
il 22 Apr 2013
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 Commenti
Kelly Kearney
il 22 Apr 2013
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 Commenti
Walter Roberson
il 22 Apr 2013
Try
axis image
and if that is not satisfactory,
axis xy
and if not that then
axis ij
Vedere anche
Categorie
Scopri di più su Annotations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!