# MATLAB: how to plot a matrix as an nxn table?

I have a routine that iteratively changes the structure of a matrix and I would like to animate the process, so that a user can actually see the structure changing.

If my matrix is nxn, I want to display the matrix as an nxn table

e.g.

Then using the 'plot' command I would like to have a figure containing:

(gridlines not necessary) My actual matrix could be 25x25 / 100x100

## Answers

This short piece of code

n = 25 ; A = randi(100, n, n) ; figure ; xlim([0 n+2]) ; ylim([0 n+2]) ; axis off ; tt = cell(n,n) ; for i = 1:n for j = 1:n tt{i,j} = text(j,n-i,sprintf('%d',A(i,j))) ; end end for k = 1:100 % Get random coordinates i = randi(n) ; j = randi(n) ; % Get random value v = randi(100) ; % Remove & update text delete(tt{i,j}) ; tt{i,j} = text(j,n-i,sprintf('%d',v)) ; % Force figure plot drawnow() ; % Wait a bit pause(0.02) ; end

prints the matrix on a grid, without the lines. It then randomly finds coordinates and change their value. I'm not sure adding lines help clarity. If really needed, you can simply add them using plot.

You can adjust the sprintf so that it displays your data nicely. If number are all on top of each other, you can increase the size of the figure by simply dragging the corner of the window.