Create a figure -exactly- a certain size
Show older comments
Consider the following code:
N = 2000;
[X,Y] = meshgrid(linspace(0,2*pi,N),linspace(0,2*pi,N));
Z = sin(X+Y);
p = pcolor(X,Y,Z)
Effectively, this colors each of the NxN elements of the matrix Z a given color. If you're running this, change N to something smaller.
I want to generate a .png figure that is exactly N x N pixels, each pixel representing one of the elements of Z.
How would I do that?
Additional notes:
This almost does it.
set(p, 'EdgeColor', 'none');
set(gca, 'Visible', 'off')
set(gcf, 'Units', 'pixels', [100 100 N+200 N+200]);
set(gca, 'Units', 'pixels', [50 50 N N]);
export_fig 'test' -png
There are two problems with this code:
- If N is large, then the figure is the incorrect dimension. For example, if N = 2000, then it seems that the figure gets expanded only until the height of your screen is reached. I need to be able to create arbitrarily sized images. The screen display is not important.
- The resultant figures are N+2 times N+2 pixels. I think that it's the 'removed' axes borders. How do I make it exactly N x N pixels (without cropping or re-sizing with an image editor)?
3 Comments
What are you using the images for that you can't afford to have the 2 extra pixel rows in each dimension?
Does this help with the N=2000 issue?
set(FigureHandleName,'Position',[1 1 2000 2000])
Theo
on 30 Jul 2012
Accepted Answer
More Answers (0)
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!