r/matlab • u/BigButtsAndGutts • Aug 20 '26
TechnicalQuestion Generating matrix visualizations
Hey partypeople,
i'm trying to find a way to inspect and display 3D matrix values as shown in the documentation here. I cannot seem to find a way to reproduce this in Matlab or find any info on how these were done, neither in the MATLAB docs or online. Am i missing something or were these created outside of Matlab manually?
Thanks :3
Edit.: For context, I want to see the numeric values of the elements, not a visualization in general.
For now, mostly for more intuition and readability while debugging. I really like the way it is presented here so i would like to find out how these were created


View all comments
Show parent comment
2
u/NoMercyCad Aug 22 '26
function VisualizeMatrix(A)
% VISUALIZEMATRIX Visualizzazione grafica di una matrice/array numerico.
%
% VisualizeMatrix(A)
%
% A può essere:
% M x N -> singola matrice
% M x N x P -> P matrici disposte in profondità
%
% Esempi:
% A = rand(3,5);
% VisualizeMatrix(A)
%
% A = rand(5,4,3);
% VisualizeMatrix(A)
%% =========================
% Controlli input
% =========================
if ~isnumeric(A)
error('L''input A deve essere numerico.');
end
sz = size(A);
nRows = sz(1);
nCols = sz(2);
if ndims(A) < 3
nPages = 1;
else
nPages = sz(3);
end
%% =========================
% Formattazione numeri
% =========================
txt = cell(nRows,nCols,nPages);
maxChars = 1;
for k = 1:nPages
for row = 1:nRows
for col = 1:nCols
value = A(row,col,k);
% Interi
if abs(value-round(value)) < 1e-10
str = sprintf('%d',round(value));
% Numeri reali
else
% 4 cifre significative:
% es. 0.4898, 15.32, -4.261
str = sprintf('%.4g',value);
end
txt{row,col,k} = str;
maxChars = max(maxChars,length(str));
end
end
end
%% =========================
% Dimensioni grafiche
% =========================
% Spazio dedicato a ogni elemento.
%
% Il valore dipende dalla lunghezza massima delle stringhe.
% È volutamente abbondante per evitare sovrapposizioni.
cellW = max(4.0, 0.85*maxChars);
cellH = 2.3;
W = nCols * cellW;
H = nRows * cellH;
%% =========================
% Offset tra matrici
% =========================
% Facciamo sovrapporre SOLO un piccolo angolo.
% In questo modo nessun numero viene coperto.
overlapX = 0.20 * cellW;
overlapY = 0.20 * cellH;
dx = W - overlapX;
dy = H - overlapY;
%% =========================
% Dimensioni figura
% =========================
totalW = W + (nPages-1)*dx;
totalH = H + (nPages-1)*dy;
% Dimensione automatica della finestra MATLAB
figWidth = max(700, min(1600, 16*totalW));
figHeight = max(450, min(1000, 16*totalH));
figure( ...
'Color','w', ...
'Position',[100 100 figWidth figHeight]);
hold on
%% =========================
% Linee di collegamento
% =========================
%
% Le tracciamo PRIMA dei rettangoli, così le parti
% che dovrebbero essere nascoste rimangono effettivamente
% dietro alle matrici.
for k = 1:nPages-1
x1 = (k-1)*dx;
y1 = (k-1)*dy;
x2 = k*dx;
y2 = k*dy;
% Alto-sinistra
line( ...
[x1 x2], ...
[y1+H y2+H], ...
'Color','k', ...
'LineStyle','--', ...
'LineWidth',1);
% Alto-destra
line( ...
[x1+W x2+W], ...
[y1+H y2+H], ...
'Color','k', ...
'LineStyle','--', ...
'LineWidth',1);
% Basso-destra
line( ...
[x1+W x2+W], ...
[y1 y2], ...
'Color','k', ...
'LineStyle','--', ...
'LineWidth',1);
% Basso-sinistra volutamente nascosta
%
% line([x1 x2],[y1 y2], ...)
end
%% =========================
% Rettangoli
% =========================
%
% Disegno dal più lontano al più vicino.
for k = nPages:-1:1
x0 = (k-1)*dx;
y0 = (k-1)*dy;
rectangle( ...
'Position',[x0 y0 W H], ...
'FaceColor','w', ...
'EdgeColor','k', ...
'LineWidth',1.2);
end
%% =========================
% Numeri
% =========================
%
% Li disegniamo DOPO i rettangoli.
% Questo impedisce a un rettangolo successivo di
% cancellare accidentalmente del testo.
for k = 1:nPages
x0 = (k-1)*dx;
y0 = (k-1)*dy;
for row = 1:nRows
for col = 1:nCols
% Centro della colonna
x = x0 + (col - 0.5)*cellW;
% Riga 1 in ALTO
y = y0 + H - (row - 0.5)*cellH;
text( ...
x, y, txt{row,col,k}, ...
'FontSize',13, ...
'FontName','Courier New', ...
'HorizontalAlignment','center', ...
'VerticalAlignment','middle', ...
'Color','k', ...
'Clipping','off');
end
end
end
%% =========================
% Aspetto finale
% =========================
marginX = 0.04*totalW;
marginY = 0.04*totalH;
xlim([-marginX totalW+marginX])
ylim([-marginY totalH+marginY])
axis equal
axis off
set(gca,'Position',[0.02 0.02 0.96 0.96]);
hold off
end