Base on Jake's answer here is a MATLAB script that returns a TikZ figure:
Input arguments:
- MATRIX: a [m x n] matrix (indices used as positions) or a [m x 3] [x y data] (if 4e arg. is
list).
- FILENAME: output file.
- WIDTH: width of the figure.
- LIST: if equal to
list treats the matrix as a [m x 3] matrix.
function matrix2TikZ(matrix, filename, width, list)
%MATRIX2TIKZ Convert matrix to TikZ/Pgfplots figure.
%
% MATRIX2TIKZ(MATRIX, FILENAME,...)
% convert the 2d MATRIX to LaTeX figure saved in FILENAME.
% MATRIX is a [n x m] or [m x 3] matrix.
%
% MATRIX2TIKZ(MATRIX, FILENAME, WIDTH) explicitely specifies the width of the
% figure. (default: 8cm)
%
% MATRIX2TIKZ(MATRIX, FILENAME, WIDTH, list) if list == 'list' the MATRIX is
% already a [m x 3] matrix and is directly written to file.
%Input checks
if ndims(matrix) ~= 2
error('I can only convert a 2D matrix.');
end
if nargin < 2
error('You must provide at least two arguments: a MATRIX and a FILENAME.');
elseif nargin < 3
width = '8cm';
list = '';
end
%Generate output matrix [x y data]
if strcmp(list, 'list')
out = matrix;
else
out = zeros(size(matrix,1)*size(matrix,2),3);
for x=1:size(matrix, 2)
for y=1:size(matrix, 1)
%Y axis is inverted
out((y-1)*size(matrix, 2)+x,:) = [x size(matrix,1)+1-y matrix(y,x)];
end
end
end
%Write to file
fileID = fopen(filename,'w');
fprintf(fileID,[ ...
'\\pgfplotsset{\n' ...
' matrix plot/.style={\n' ...
' axis on top,\n' ...
' clip marker paths=true,\n' ...
' scale only axis,\n' ...
' height=\\matrixrows/\\matrixcols*\\pgfkeysvalueof{/pgfplots/width},\n' ...
' enlarge x limits={rel=0.5/\\matrixcols},\n' ...
' enlarge y limits={rel=0.5/\\matrixrows},\n' ...
' scatter/use mapped color={draw=mapped color, fill=mapped color},\n' ...
' scatter,\n' ...
' point meta=explicit,\n' ...
' mark=square*,\n' ...
' cycle list={\n' ...
' mark size=0.5*\\pgfkeysvalueof{/pgfplots/width}/\\matrixcols\n' ...
' }\n' ...
' },\n' ...
' matrix rows/.store in=\\matrixrows,\n' ...
]);
fprintf(fileID, [ ...
' matrix rows=%i,\n' ...
], size(matrix,1));
fprintf(fileID, [ ...
' matrix cols/.store in=\\matrixcols,\n' ...
]);
fprintf(fileID, [ ...
' matrix cols=%i,\n' ...
], size(matrix,2));
fprintf(fileID, [ ...
'}\n' ...
'\\begin{tikzpicture}\n' ...
' \\begin{axis}[\n' ...
]);
fprintf(fileID, [ ...
' width=%s,\n' ...
], width);
fprintf(fileID, [ ...
' matrix plot,\n' ...
' colormap/hot,\n' ...
' colorbar\n' ...
' ]\n' ...
' \\addplot table [meta=peaks]{\n' ...
' time channel peaks\n' ...
]);
fprintf(fileID, [ ...
' %f %f %f\n' ...
], out');
fprintf(fileID, [ ...
' };\n' ...
' \\end{axis}\n' ...
'\\end{tikzpicture}' ...
]);
fclose(fileID);
The code is also hosted on github.
imagesc(data, [min max]), whereminandmaxwould be the same for all your plots. – Jake Jul 16 '13 at 17:15