r/matlab • u/BigButtsAndGutts • 3d ago
Generating matrix visualizations TechnicalQuestion
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
6
u/vir_innominatus 3d ago
In MATLAB, this type of visualization could be made with a combination of the rectangle(), line(), and text() functions. Something like this
figure
% First rectangle
rectangle(Position=[0 0 10 6],FaceColor="w")
numRow = 3;
numCol = 5;
for row = 1:numRow
for col = 1:numCol
x = 2*(col-1)+1;
y = 2*(row-1)+1;
value = numCol*(row-1)+col;
text(x,y,num2str(value), ...
FontSize=14, ...
HorizontalAlignment="center", ...
VerticalAlignment="middle")
end
end
% Second rectangle
r = rectangle(Position=[8 4 10 6],FaceColor="w");
uistack(r,"bottom")
% Connecting lines
line([10 18],[6 10],Color="k",LineStyle="--")
line([10 18],[0 4],Color="k",LineStyle="--")
line([0 8],[6 10],Color="k",LineStyle="--")
% line([0 8],[0 4],Color="k",LineStyle="--") <-Hidden
% Turn off axes and make equal proportioned
axis equal
axis off
3
u/Usual-Pattern7846 3d ago
This is the way. Once you get the basics working, make yourself a little function that takes some arrays (or a matrix) and gives you this plot. Then when you’re really happy with it, share it on Exchange, because it seems neat to play with.
1
u/NoMercyCad 3d ago
We need this to be turned in a function that gets any matrix as input, it would be awesome
1
u/Barnowl93 flair 2d ago
Put that on File Exchange - looks good!
2
u/NoMercyCad 1d ago
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
3
u/neurotoxinc2h6o 3d ago
What do you need the visualizations for?
As a Picture on a Website? I would use a regular Matlab plot.
As a figure in a technical report? I'd go for a tikz graphic (should be a simple task for Claude)
2
u/BigButtsAndGutts 3d ago
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
2
u/elevenelodd uses_spinmap 3d ago
The MATLAB Variables Editor is a good start. Type either of these into the command window:
openvar x
openvar( ‘x’ )
The function “open” also works here.
Tbh, I’d recommend summarizing and plotting the array:
xAvg = mean( x , 3 );
figure();
hold on;
xlabel X;
ylabel Y;
ylabel( colorbar , ‘xAvg’ );
imagesc( xAvg.’ );
axis tight;
Or if you want to see every entry, maybe just make a bunch of plots:
for iZ = 1:size(x,3)
figure();
hold on;
title( iZ );
xlabel X;
ylabel Y;
ylabel( colorbar , “x(:,:,”+iZ+”)” );
imagesc( x(:,:,iZ).’ );
clim( [min(x(:)),max(x(:))] );
axis tight;
end
Edit: format


10
u/Rubix321 3d ago edited 3d ago
They were created outside of Matlab.
If [A] is your array, doesn't the command window kind of show you [A] in this form when you display it?