78 views (last 30 days)

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)?

Oliver Woodford
on 8 Aug 2012

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/44711-create-a-figure-exactly-a-certain-size#comment_91924

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/44711-create-a-figure-exactly-a-certain-size#comment_91924

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/44711-create-a-figure-exactly-a-certain-size#comment_91943

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/44711-create-a-figure-exactly-a-certain-size#comment_91943

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/44711-create-a-figure-exactly-a-certain-size#comment_92205

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/44711-create-a-figure-exactly-a-certain-size#comment_92205

Sign in to comment.