Save matrix as a spreadsheet image (in previous versions)

1 view (last 30 days)
A couple of weeks ago, I asked how one could export a table to an image of the table, linked here,
That solution worked for me, but the client's version of MATLAB is R2016b. The tool used in this answer was exportapp, which was introduced in R2020b. Is there an alternative that is compatible with previous versions?
Thanks!
  3 Comments
Varun Kumar
Varun Kumar on 26 Jul 2022
I'm fine with any font, honestly. I just need a table exported to an image :)
Thank you!

Sign in to comment.

Answers (1)

DGM
DGM on 26 Jul 2022
Edited: DGM on 26 Jul 2022
This is what I used to create the first image:
tablesize = [3 4]; % size of table
A = rand(tablesize); % some test data to fill the table
font = 'ibm-vga-16x9'; % font used by textim()
cellsize = [20 110]; % in pixels (depends on font and # of chars)
cornerpad = [0 1]; % controls text offset from SE corner of cell
borderw = 5; % width of border lines
% build cell array of text images
ncel = prod(tablesize);
C = cell(ncel,1);
for k = 1:ncel
thiscell = textim(sprintf('%.5f',A(k)),font); % generate image of text
C{k} = padarray(thiscell,cornerpad,0,'post'); % offset SE corner
end
% stack images on dim4, align to SE corner, pad to specified size
C = imstacker(C,'padding',0,'gravity','se','size',cellsize);
C = padarray(C,[1 1]*borderw,1,'pre'); % add 1px border on N and W edges of each cell
outpict = imtile(C,tablesize); % tile images (THIS IS NOT IPT IMTILE)
outpict = padarray(outpict,[1 1]*borderw,1,'post'); % close borders on S and E edges
outpict = iminv(outpict); % invert to suit taste
imshow(outpict)
imwrite(outpict,'op1.png')
The second image is basically the same, but I used Rik's text2im() instead of MIMT textim().
tablesize = [3 4]; % size of table
A = rand(tablesize); % some test data to fill the table
font = 'droid_sans_mono'; % font used by text2im()
cellsize = [110 672]; % in pixels (depends on font and # of chars)
cornerpad = [0 15]; % controls text offset from SE corner of cell
borderw = 5; % width of border lines
% build cell array of text images
ncel = prod(tablesize);
C = cell(ncel,1);
for k = 1:ncel
thiscell = text2im(sprintf('%.5f',A(k)),font); % generate image of text
C{k} = padarray(thiscell,cornerpad,0,'post'); % offset SE corner
end
% stack images on dim4, align to SE corner, pad to specified size
C = imstacker(C,'padding',0,'gravity','se','size',cellsize);
C = padarray(C,[1 1]*borderw,1,'pre'); % add 1px border on N and W edges of each cell
outpict = imtile(C,tablesize); % tile images (THIS IS NOT IPT IMTILE)
outpict = padarray(outpict,[1 1]*borderw,1,'post'); % close borders on S and E edges
outpict = iminv(outpict); % invert to suit taste
outpict = imresize(outpict,0.5); % downscale if desired (this is essentially antialiasing)
imshow(outpict)
imwrite(outpict,'op1.png')
Both of these examples rely on tools from MIMT:
Bear in mind that this was all off-the-cuff on my part. If the cell formatting or justification needs to be changed, the script will need to be modified.
The basic idea is to generate a set of images containing just the text. Each image may vary in width if the number of characters differ. When using imstacker(), the images are merged into a 4D array with common page geometry. So long as the specified cell size is larger than all the text images, everything should be okay. The justification is controlled by setting the gravity, though flipping gravity may also require the cornerpad application to be swapped to 'pre'.
The 4D stack has half of the borders added before tiling. The remaining borders are closed after tiling. Note that MIMT imtile() will have a name conflict with IPT imtile(). It should work if it has precedence on the path, but if you want to use IPT imtile() for other things, be aware that it will be shadowed.
The cellsize parameter needs to be determined based on the expected text length and the character size. Both text2im() and textim() use monospace fonts and the synopses list the character size for each font.
  1 Comment
DGM
DGM on 26 Jul 2022
This is totally tangential and unrelated to your needs, though perhaps future passers-by may be entertained. At first, the question title reminded me of one of the earliest additions to MIMT, added before MIMT was ever published. Whereas you needed a tool to create an image of a spreadsheet, MIMT im2ods() generates spreadsheets from images:
A = imread('peppers.png');
im2ods(A,'peppers.ods',0.06,100);
It really has no practical use, but it was fun to be reminded.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by