MATLAB: Displaying an axis scale in mm rather than pixels

axis scales

I have written a code for image analysis that uses the number of pixels throughout to calculate lengths. Now I want to plot graphs and have my axis reading in millimetres rather than number of pixels. I have a calibration factor calculated, is there a simple way I can have axis in millimetres. I need this code to work for multiple pictures therefore using the xticks and xticklabels method would help.

Best Answer

It's a bit tricky without any example data, but this should do the trick.
conversion=0.1; % in mm/pixel
addMM=@(x) sprintf('%.3fmm',x*conversion);
xticklabels(cellfun(addMM,num2cell(xticks'),'UniformOutput',false));
yticklabels(cellfun(addMM,num2cell(yticks'),'UniformOutput',false));
You can fine tune how many digits are displayed by editing the '%.3fmm' in the second line(see formatSpec). Change the variable conversion to your needs. It's in mm per pixel. Hope that helps, if not, just ask. Note that Matlab still treats the axis in Pixel, but displays the converted mm. If you make changes to xtick and ytick, you have to call the above lines again.