You can use insertText function, to insert text into an image.
Here is my code sample:
A = [17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9];
[n_rows, n_cols] = size(A);
%Set background color to [240, 240, 240] (light gray).
I = zeros(n_rows*24+12, n_cols*10*5+8, 3, 'uint8') + 240; %Background color
text_str = cell(n_rows, 1);
box_color = zeros(n_rows, 3) + 240; %Set box color to same as background color
text_color = repmat([125, 39, 39], n_rows, 1); %Set text color to brown.
%Fill rows of A into cell array text_str
for i = 1:n_rows
text_str{i} = sprintf('%5d', A(i, :));
end
%Set position of each of the rows.
pos_x = zeros(1, n_rows) + 4;
pos_y = (0:n_rows-1)*24 + 6;
position = [pos_x', pos_y'];
%Insert text into image I.
I = insertText(I, position, text_str, 'FontSize', 14, 'BoxColor', box_color, 'TextColor', text_color,...
'Font', 'Courier New Bold', 'BoxOpacity', 1);
figure;imshow(I);
Result:

Aligned text with different font:
Applying my solution for aligning text with no fixed width, requires different coordinate for each number (total of 25 coordinated and 25 strings).
The following solution uses Arial font:
A = [17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9];
[n_rows, n_cols] = size(A);
n_elms = numel(A);
%Set background color to [240, 240, 240] (light gray).
I = zeros(n_rows*24+12, n_cols*10*5+8, 3, 'uint8') + 240; %Background color
text_str = cell(n_elms, 1);
box_color = zeros(n_elms, 3) + 240; %Set box color to same as background color
text_color = repmat([125, 39, 39], n_elms, 1); %Set text color to brown.
%Fill rows and columns of A into cell array text_str
counter = 1;
for y = 1:n_rows
for x = 1:n_cols
text_str{counter} = sprintf('%5d', A(y, x));
counter = counter + 1;
end
end
%Set position of each of the rows and columns.
[pos_x, pos_y] = ndgrid(0:n_cols-1, 0:n_rows-1);
pos_x = pos_x(:)*10*5 + 4;
pos_y = pos_y(:)*24 + 6;
position = [pos_x, pos_y];
%Insert text into image I.
I = insertText(I, position, text_str, 'FontSize', 14, 'BoxColor', box_color, 'TextColor', text_color,...
'Font', 'Arial Bold', 'AnchorPoint','RightTop', 'BoxOpacity', 1);
figure;imshow(I);
Result:
