File Exchange

image thumbnail


version (3.25 KB) by Iram Weinstein
Create cross-hatched filled regions.


Updated 07 Aug 2002

No License

This is an old file by Kirill K. Pankratov thaat used to be on the file exchange. I've updated it to run on Matlab 5+.

Cite As

Iram Weinstein (2021). hatch.m (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (29)

Dominik Mattioli

Awesome hack; needs clearer doc, especially for using a legend.


can anyone give an exmaple how to use this file?

daoxian Lu

Luisa Barrera

Same problem:
i'm confused by the error: object must be either line or patch.
no idea how to fix this problem.

fei he

i'm confused by the error: object must be either line or patch.
no idea how to fix this problem.

Vineet Vishwakarma

Please suggest how to fill the bar graph here. Thanks in advance.

Y = xlsread('Data');
X = Y(:,1); Y1 = Y(:,2); Y2 = Y(:,3);

figure('Name','Data - Bar Chart');
hold on
legend('Data 1','Data 2')


Ahmed raafat





chen rui

Lao Li

yipeng Wang


good job

Qingli Dong

good job

Bruce Wang

good job

rong zhang

xianlong liu

good job, could be a good idea


The code works pretty well. I cannot get it to work with the legend, though.
Here what I am trying:

hp = findobj(h,'type','patch');


Any suggestion?


This is to my knowledge the best function to use if you hatch patches need to have 'holes' in them (e.g. a torus-shaped hatch patch). Works on R2014a.

When I plotted complex hatch plots, there would be dotted lines running across different hatch patches. This is fixed by inserting the following code immediately after the "Organize to pairs and separate by NaN's" block:

% Remove point line segments (i.e. segment of the form (a,b) to (a,b)
i_same = find(diff(xi)<eps('single')); % find the first index of each point
i_remove = repmat(i_same,[3 1]) + repmat((0:2)',[1 length(i_same)]);
xi(i_remove) = [];
yi(i_remove) = [];

This amendment removes hatch lines shorter than 1E-7 unit long (essentially what those dotted lines are).

Neil Dalchau

Generally works, but could not get the angle to work. I'm using Matlab on Windows 7, 32-bit.

Scott Beaver

This didn't work for me-- the hatch lines would not rotate in my figures, and no hatch lines were drawn using "hatch demo." Perhaps an issue with my Linux_64 machine?

Martin Mul

Nice program, but i have one question:
If I hatch it hatches to one side of my graph. How do i manage to hatch also the other side of this graph?

Bob Farrell

Now need to include 'v6' as first arg to bar function in order for demo to work. Even though this was exactly what I was looking for (and amazed that this isn't built in), I ended up not using it partly because I didn't understand why exactly horizontal or exactly vertical lines jump one pixel -- wasn't asthetic to my eye for manuscript quality.

Joel Lindop

Very useful, thanks a lot!
One tiny change worth making: the function definition on the first line is missing its output variables. Change to:
function lh = hatch(obj,angle,color,style,step,width)
if you want access to the handle (lh).

Dimitri Pavlidis

Gene Smith

I have used this to create complex bar graphs. It took a while to figure out how to use it. My usage was

hdl = patch(x,y,'w','EdgeColor'.'k'); % using MATLAB "patch" to establish the border


I did notice at cetain angles some of my small bars had the hash lines at strange angles.

Troy Goodson

I've done some limited testing of this
m-file with Matlab (R13.0.1)
and it's been working very well!
Thanks so much Iram!

Steve Chiswell

Usefull m file. I often need hatched shading for b&w figures in journals. Hopefull this will do the job.
Took me a while to figure out how to run it

MATLAB Release Compatibility
Created with R11.1
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: stipple, Hatchfill

Community Treasure Hunt

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

Start Hunting!