MATLAB: How to format the output using fprintf to show a variable with a user defined amount of decimal places (this amount is stored in a variable)

format ouputfprintfMATLABoutputsprintfvariable output

How can I format my output using fprintf to show a variable with a user defined amount of decimal places (this amount is stored in a variable)?

Best Answer

Example:
N = 4;
fprintf( '%.*f', N, pi)
On output, the * signals that the number of decimal places is to be read from a parameter.