Fine-Tuning Array Display
Researchers often need to incorporate MATLAB computation results into tables in documents. Directly copying and pasting raw output from the command window is not a good idea, as it may not provide the numbers in the desired format, and manual copying and pasting can lead to errors and waste time if recalculation is needed.
It is better to let MATLAB print the output in an exact format. <span>print_matrix</span> function can accomplish this task. This function offers the following options: print output to a file, remove unnecessary zeros from the exponent part, print the matrix in the format of LATEX’s <span>tabular</span> environment, and format each column to a given field width (the function does not give warnings if this causes numbers to be truncated). Here are some examples:
>> x = [0 1.999pi*1e53.959*1e20-5.4555*1e100];
>> A = [x; 1./(x+eps)]
A =
0.00e+002.00e+003.14e+053.96e+20-5.46e+100
4.50e+155.00e-013.18e-062.53e-21-1.83e-101
>> print_matrix(A)
0.00e+002.00e+003.14e+053.96e+20-5.46e+100
4.50e+155.00e-013.18e-062.53e-21-1.83e-101
>> print_matrix(A,{"%6.2e","%3.1f","%7.3e","%4.1e","%g"})
0.00e+002.03.142e+054.0e+20-5.4555e+100
4.50e+150.53.183e-062.5e-21-1.83301e-101
>> print_matrix(A,'%4.1e',[],9,1,1) % LaTeX format, remove +, zeros.
0.0 & 2.0 & 3.1e5 & 4.0e20 & -5.5e100 \\
4.5e15 & 5.0e-1 & 3.2e-6 & 2.5e-21 & -1.8e-101 \
print_matrix
function print_matrix(x, fmt, file, fw, ltx, ex)
%PRINT_MATRIX Print formatted matrix to command window or file.
% PRINT_MATRIX(X, fmt, file, fw, ltx, ex) prints matrix X according to the fprintf style format specified by the string fmt.
% Default: fmt = '%9.2e'.
% If fmt is a cell array of strings, its j-th element will be used to format the j-th column, reusing elements as necessary.
% fw: field width for each number (default: automatic).
% file: output filename, or empty to output to command window.
% ltx = 1 uses LaTeX tabular format, otherwise ltx = 0 (default).
% ex = 1 removes the plus sign and leading zeros in the exponent, otherwise ex = 0 (default).
if nargin < 2 || isempty(fmt), fmt = '%9.2e'; end
if nargin < 3, file = []; end
if nargin < 4, fw = []; end
if nargin < 5 || isempty(ltx), ltx = 0; end
if nargin < 6, ex = 0; end
if isstr(fmt), fmt = {fmt}; end % Ensure fmt is a cell array.
[m, n] = size(x);
if isempty(file)
fid = 1; % Output to screen.
else
fid = fopen(file, 'w');
end
% Conversion for removing the plus sign and leading zeros in the exponent.
exps = {'e+''e'; 'e00 '' '; 'e0''e'; 'e-00''e-'; 'e-0''e-'};
for i = 1:m
for j = 1:n
fmtj = fmt{1 + mod(j - 1, length(fmt))}; % Format string for the j-th column.
str = sprintf([fmtj ' '], x(i, j));
% Remove the plus sign and leading zeros in the exponent
if ex
for k = 1:length(exps)
str = strrep(str, exps{k, 1}, exps{k, 2});
end
end
% Field width adjustment
if fw
str = [str char(32 * ones(1, fw))];
str = str(1:fw);
end
fprintf(fid, '%s ', str);
% LaTeX format
if ltx && j < n, fprintf(fid, '& '); end
end
% End of LaTeX format
if ltx, fprintf(fid, '\\'); end
fprintf(fid, '\n');
end
% Close file
if ~isempty(file), fclose(fid); end