File Exchange

image thumbnail

Myaa - My Anti-Alias for Matlab

version (268 KB) by Anders Brun
Better looking Matlab figures with anti-aliasing!


Updated 21 Aug 2008

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Add myaa.m to your path and enjoy anti-aliased professionally looking graphics in Matlab at any time. Myaa works with any kind of graphic (3-D, plots, scatterplots, ...) and even adds anti-aliasing to text, ui controls and grids. Myaa is ideal for complex, cluttered and saturated plots.

See attached screenshot for a demonstration. More examples included in the code, just run 'help myaa'.

For those of you who 'publish' your code often, an undocumented anti-aliasing option is included in the snapnow.m function in Matlab. To publish a file called test.m you can do:

opts.figureSnapMethod = 'antialiased';

However, you will have more control over the process using myaa, which is also the best choice when using Matlab interactively.

In any case: Anti-aliasing is good for you! :-)

Cite As

Anders Brun (2021). Myaa - My Anti-Alias for Matlab (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (70)

Marco Pizzoli

Dhruba Roy

Jeroen Aeles

Anyone else having issues with this function in R2018b version of Matlab? Have been using this function since 2012 and always worked great but now it is making my figures horrible (very low quality)...
Any help appreciated :)



Can I apply the antialiasing to just a single axes in a figure, rather than the whole figure? I want vectors to be preserved in some parts of the figure when printing to pdf.

Bill Tandy

Zhou Jingren

OMG! That's terrific and exactly what I want.

Daniel Mlakar


Thanks for your wonderful job.

Yongjun Wang


Thanks for a job well done ...


Awesome! Looks neat! Life saver!

Sojeong Lee

Really thank you. It is the only one that I was looking for.
And thank you for notifying how to save figures, DGM :-)


Oh the times I could have used this...

The results look great, and it's simple to use. For anyone trying to figure out how to save figures, you can always enable the figure menu:



Really great


Thank you very much, it produces gorgeous plots

Tibor Stanko



Christopher Jones

There is a problem that non-visible components of a gui are made visible during the process and appear in the saved aa-image. I cannot quite work it out.
It happens in this row:
print(self.source_fig,['-r',num2str(1*screen_DPI*self.K(1))], '-dpng', tempfile);

Appart from that, for non GUI export I love this function to bits!



Masoud Ghanbari

Interesting Trick ;)


Hi there,

Like the images that this function produces, but is there any way to get it so that the plot tools do not disappear and options like save etc. Tried to save many of these as eps files, but they don't save as well as normal matlab plots.

Any ideas?


I'm having trouble with vertical plot labels using the LaTex interpreter. A command like this:

ylabel('$v$ (\textit{pixels})','Interpreter','LaTex','FontSize',12);

Doesn't render correctly using myaa. The v displays correctly, but the "(pixels)" just disappears. The xlabel works correctly though.

Any ideas how to get this right?



Anders Brun

if you like myaa, please have a look att aacam to! :-)

John Blommers

There appears to be a bug. Under certain conditions of the arguments raw_lowres is undefined and myaa errors out. See below:

Undefined function or variable "raw_lowres".

Error in myaa (line 226)
sz = size(raw_lowres);

Jessica Kalynn

Figure looks fabulous but when I use the saveas function on it, it only saves the bottom right corner (assuming because the image is quite large). Help?!

I used this snipit of code:

[f1, cdata] = myaa([4, 2]);
imwrite(cdata, 'runtest.png', 'png');


nice one!

Sashankh Rao

Great tool! Thanks! Quick question: I have dashed lines (--)in my plot. When I use this tool it returns a straight line. Is there a fix for this?



Only reason doesn't get 5 stars is cause saving isn't built in

ubuntu21 T

Thank you for sharing this nice code.

To easily have the image saved on my hdd, I modified 2 lines in original code:

1. 192# %delete(tempfile);
meaning that I have commented this line, such that the file with the image should not be removed

2. 182# tempfile = ['myaa_temp_screendump ',num2str(rand(1)*1e5,'%5.0f'), '.png'] ;
meaning that each time I run myaa.m, the image name should change, ending with 5 random numbers. Therefore, I do not overwrite the same image file each time I run myaa.m


Can someone remind me how to execute myaa.m? when I viewed the source code. it says I can run it simply as "myaa;" and it will operate on the "current figure". As a tests. I cropped the screenshot that came in the zip and saved the unfinished left side as it's own jpg. I then used P = imread(<filename>) to import the jpg I just saved, imshow(P) to put the images as the "current figure" then myaa. figure 1 was the orignal image as I expected but figure two (the output of myaa) was the same eyesore image I loaded. It's as if myaa did not get applied to it.

the sample code I've been running is:

I = imread('C:\Documents and Settings\amsowchu\Desktop\myaa\myaa\test.jpg');


Since this discussion seems to be still up to date...

Why not simply override the rendering settings for matlab in the graphics driver?

Using my nVidia card i simply start the nVidia control center, add matlab.exe as a
program using custom settings and choose "Override all application settings" (i hope this translation from my german graphics driver is correct) and set

Antialiasing-transparency = Supersampling
Antialiasing-setting = 4 or 8 or ....

I cannot tell if it works well with everything you can plot in matlab - but so far it does.


Hi, Thanks for this tool, the rendering is great, however I have a problem with the text font size. In contrast with what David said (Jan 2010), mine is absolutely tiny (actually totally unreadable), which is really too bad.

Is there a way to fix this ?

Thanks in advance


To add a few code , it will improve much.

1. Double resolution of temp PNG

185# print(self.source_fig,['-r',num2str(2*screen_DPI*self.K(1))], '-dpng', tempfile);

2. Then, get handle of image in figure

231# hi = image(raw_lowres);
... hi = image(...

3. Save image matrix

290# elseif nargout == 2
varargout(1) = {fig};
varargout(2) = {get(hi, 'CData')};


Test program

surf(peaks(60) ,'FaceColor','interp', 'FaceAlpha',0.8,'EdgeAlpha',0.8, 'LineWidth', 0.2);

set(gcf, 'Color', [1,1,1]);
[f1, cdata] = myaa([4, 2]);

imwrite(cdata, 'runtest.png', 'png');


It's high resolution picture !

stephen Strenn

Great tool. However I seem to get the following error for figures larger than, say, 640x480. Any suggestions?

??? Error using ==> conv2
Out of memory. Type HELP MEMORY for your options.

Error in ==> myaa at 198
mm = myconv(ones(size(self.raw_hires(:,:,1))),kk,'same');


Awesome, it's great, thank you;-)

Jonas Lundgren

Thank you! This is useful.

Dave Holden

Rob Campbell

Nice. This is the sort of thing that should come built in to Malab. In general, the display of 3-D plots in Matlab leaves much to be desired.


Hello Anders,

Very nice thank you!

I found it increases the text size of the original graphic - is this expected or have I found a 'feature'?

Thanks again,

Daniel Armyr

I give it a 4 out of five becaus it is slow as a doornail.
I was wondering about one step that might not be strictly necessary, and that is line 198
mm = myconv(ones(size(self.raw_hires(:,:,1))),kk,'same');
Here you do a convolution on what is normally a huge matrix filled with just ones. I assume you do this to get the edge behaviour. If that is the case, you could just do it on a smaller matrix and then expand the middle row and middle column to get a larger image. My huch says that will save you an entire second of the 8 it takes on my comp.

Also, I think there is potential in using the grabframe() instead of print, but I see problems in making a high-res copy without side-effects, so that one will have to simmer a bit.

Linda F


great submission! How do you set a high super-sampling factor, a low down-sampling factor, and still get the full image? When I do those, myaa automatically zooms in and I only get some portion of the image..





Anders, Could you use this while making a movie (say with getframe)? I havent tried it yet, but it looks like - if you can, it will probably take infinite time to do that...
Nevertheless, this is a beautiful application! Thanks!

Anders Brun

Hi Sebastian,
I already know of LineSmoothing and I created Myaa because I needed something more general. LineSmoothing does exactly what is says, it can smooth a line. If two lines crosses, things start go wrong. And there are more relevant objects in Matlab, such as text labels and (unintentionally) hires images, say a 3000x3000 image. Myaa will show you what your figure will look like in print, LineSmoothing will not. But ofcourse LineSmoothing is useful to some people and in some situations, speed is one of its advantages but generality is not.

Believe me, nobody would be more happy than me the day Matlab can do true antialiasing in hardware. :-)

Sebastian Hölz

I hate to tell you, but your file is of very limited use. Little known fact: Matlab (at least from 7.04 on) includes an AA, it is just hidden. Try the following:
>> spharm2
>> set(gco,'LineSmoothing','on')
It will give you AA on the real 3D object, i.e. paning, rotating, zooming possible in real-time, no need for time- and memory-consuming calculations. The "LineSmoothing"-property is available for all relevant HGObjects, i.e. lines, patches, surfaces.
I think the smoothing is also applied to exported and saved figures, as long as you specify ZBuffer or OpenGL as renderer.


Oliver Woodford

A nice idea that has greatly improved some figures I'm publishing.

I would have liked the function to simply save the anti-aliased output to file (png). I've now written something similar which does just that.

matt dash

very nice, but i think it should optionally take a figure handle as an input instead of just using gcf.

matt dash

Luc Masset

Fantastic. Great work. I have been looking for an anti-aliasing feature in Matlab for years.

I've just changed two small things to have myaa to work in Matlab R13 :

line 111: remove the (), I guess it always takes @conv2

line 213: I changed this way :

raw_lowres = double(imresize(self.raw_hires,1/self.K(2),'bilinear'))/256;
raw_lowres = single(raw_lowres);

Thanks again Anders.

Stefan Novak

Excellent job! Looking forward to future releases and features!

Liu Michael

Very good, wonderful...

Husam Aldahiyat

Adam Chapman

Excellent job. Thanks very much!

Nass K

It works fine, thanks.

Matt S

Andrey Krishkevich


Per Magnusson

myaa works great and is very easy to use. Thanks Anders!

Peter A.

Works great, thanks!

Anders Brun

For your information, every resubmission takes some time to propagate through Mathworks review process... so if you have problems related to the ippl() function, please implement the bugfix below. The new version should be available for download in about a day or so.

Thank you for your patience. /Anders

Anders Brun

Thanks for the suggestion Petr. I have submitted a new version now, including a small change for this:

if ippl()
myconv = @imfilter;
mypad = 'same';
myconv = @conv2;
mypad = 'valid';
myconv = @conv2;
mypad = 'valid';

i scardanzan

rating based on the posted visualization

Petr Krysl

Well done! Just a small suggestion for improvement: ippl() was not found by Matlab (probably because I do not have the image processing toolbox). This should be perhaps treated with a try, catch?

I have not looked at the code in detail, hence no rating yet.

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!