you can obtain the gradient direction for each pixel by using the second output argument of the "imgradient" function (in image processing toolbox):
[Gmag, Gdir] = imgradient(I)
The principle behind the function is to compute finite differences in x and y directions, to store the result in two arrays with same size as original image, and then to combine them by using atan2 function.
Thank you my colleague David, but I could not understand the last speech "to store the result in two arrays with same size as original image, and then to combine them by using atan2 function."