You can use unique() to find the number of unique gray levels if that's what you're after. Consider the following image with 8 gray levels:
inpict = imread('grad8-1.png');
nu = numel(unique(inpict))
But be aware that things like antialiasing and compression may mean that the number of nominal gray levels isn't the same as the actual number of unique values in the image.
inpict = imread('grad8-2.png');
nu = numel(unique(inpict))
inpict = imread('grad8-1.jpg');
nu = numel(unique(inpict))