Q:

Matlab - short format of number variable in the plot title

I am trying to place a variable into a plot title but I cannot produce formatting of 4 decimal places. How to avoid the float format in the title?
This is the code I use
subplot(3,2,1);
hist(X,10);
str=sprintf('X N=%d,Y=%d',N,Y)
M=sum(X)/N
Mean=sprintf('Mean=%0.4d',M)
title({str,Mean})

A:

You need to use %f as the format specifier for float values. Changing your code
Mean=sprintf('Mean=%0.4d',M)

to
Mean=sprintf('Mean=%0.4f',M)

will print M with 4 decimal places of accuracy. If you want to print M without any decimal places then you need to use %.0f
Mean=sprintf('Mean=%.0f',M)

%.0f will print a double or float value with 0 decimal places and appear as if you printed an integer with %d.

If your variable X has N elements than using the builtin MATLAB function mean() will produce the same output as sum(X) / N.

Note: You should be careful of your variable names. MATLAB includes a mean() function which you don't want to overwrite by calling a variable mean. MATLAB variable names are case-sensitive so Mean is ok but mean isn't.

