Search code examples
matlabmatlab-figure

How do I multiply the xticks and yticks with constant and have it displayed in the figure in Matlab 2019?


I've got a picture (a matrix of intensities for every pixel). When plotting this with matlab's imagesc the x-,y-axis are obviously in pixels. I know that 1 pixel corresponds to 0.6250 millimeter in both x- and y-direction.

How can I change only the scale in axis in the figure to show millimeters instead of pixels, i.e how do I multiply the xticks and yticks with 0.6250 and have it displayed in the figure?

I have tried:

new_x = xticks*pixel_spacing(2);

set(gca,'xticklabels', new_x)

but this results in the error "Unable to use a value of type 'matlab.graphics.axis.Axes' as an index."


Solution

  • imagesc has a syntax where you can specify the scaling of the pixels (see the documentation).

    x = [1,size(img,2)] * pixel_spacing;
    y = [1,size(img,1)] * pixel_spacing;
    imagesc(x,y,img);
    

    (assuming img is your image to display)

    The advantage over changing the tick labels is that other things plotted on top of the image will use the same coordinates, as will retrieving mouse cursor location and so forth. This might or might not be relevant, but it’s good to know.