Skip to content

Instantly share code, notes, and snippets.

@tpaschalis
Created January 2, 2018 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpaschalis/841dd4a57434ea34506c4408b13547c5 to your computer and use it in GitHub Desktop.
Save tpaschalis/841dd4a57434ea34506c4408b13547c5 to your computer and use it in GitHub Desktop.
function mat2lat(A)
filename = 'tabular.tex';
fileID = fopen(filename, 'w+');
[rows, cols] = size(A);
% Change alignment of your output with the following character
tabalign = repmat('c', 1, cols);
fprintf(fileID,'\\begin{table}[h] \n');
fprintf(fileID,'\\centering \n');
fprintf(fileID,'\\begin{tabular}{%s} \n', tabalign);
% Change formatting of your output with the following line
tabformat = repmat('%.2f & \t', 1, cols); % Define output format
tabformat = tabformat(1:end-4); % Remove last '&' char
tabformat = [tabformat ' \\\\ \n']; % Add newlines
fprintf(fileID, tabformat, A); % Spit out input matrix
fprintf(fileID,'\\end{tabular}\n');
fprintf(fileID,'\\end{table}\n');
fclose(fileID);
fprintf('Done :)\n');
end
@tpaschalis
Copy link
Author

Simple function to convert a Matlab matrix to a LaTeX table/tabular environment. Just mat2lat(A) your matrix, copy/paste to your own .tex file, and compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment