Formatted Output
Using num2str
num2str( value, precision )
Examples
value = 8/3;
matrix = [ 1 : 3; 4 : 6 ] / 3;
disp(num2str(value))
disp(num2str(matrix))
disp(num2str(value, 16))
disp(num2str(matrix, 16))
Using fprintf
fprintf( formatString, var1, var2, ... )
Format strings
- s - string
- d - decimal integer
- f - fixed floating point
- e - scientific notation
- specifying width
- specifiying precision
Examples
heading = input('Enter heading: ', 's');
numValues = 15;
values = 2 : 3.5 : 83;
avg = mean(values);
fprintf('N = %d', numValues)
fprintf('* * * %s * * *\n', heading)
fprintf('%5d : %12.8f\n', values(1), avg)
fprintf('%5d : %12.8f\n', values(15), avg/3)
fprintf('%-5d : %12.8f\n', values(21), avg/5)