How to export 500 images in one file

3 views (last 30 days)
Dominik Stolfa
Dominik Stolfa on 28 Sep 2024
Commented: dpb on 5 Oct 2024
I have around 500 images which I want to export into one .xls .word or .pdf in 3 collums. I tried putting them all in one figure but then they become extremely small. If I create many separete figures by 3, I don´t know how to export them all into one file. I tried putting all of the images into table but then the table doesn´t generate because I run out of memorry. If I were to guess, it most likely tries to put every single value of the picture into a separated cell.
How do I make pictures in the table to be saved in their pictural form?
clear all; clc; close all;
dir_img_AP=dir('**/NUSCH*AP/**/*.jpg');
dir_img_JS=dir('**/NUSCH*JS/**/*.jpg');
dir_img_LZ=dir('**/NUSCH*LZ/**/*.jpg');
n_img_AP=numel(dir_img_AP);
n_img_JS=numel(dir_img_JS);
n_img_LZ=numel(dir_img_LZ);
n_img=max([n_img_AP,n_img_JS,n_img_LZ]);
T=cell(n_img,1);
for i=1:n_img_AP
file_path_AP=[dir_img_AP(i).folder, '/',dir_img_AP(i).name];
filename_AP = dir_img_AP(i).name;
pic_AP = imread(filename_AP);
T{i,1} = pic_AP;
end
for i=1:n_img_JS
file_path_JS=[dir_img_JS(i).folder, '/',dir_img_JS(i).name];
filename_JS = dir_img_JS(i).name;
pic_JS = imread(filename_JS);
T{i,2} = pic_JS;
end
for i=1:n_img_LZ
file_path_LZ=[dir_img_LZ(i).folder, '/',dir_img_LZ(i).name];
filename_LZ = dir_img_LZ(i).name;
pic_LZ = imread(filename_LZ);
T{i,3} = pic_LZ;
end
table=cell2table(T);
arr = table2array(table);
% imshow(arr);
f_name='pic.xls';
% writetable(table,f_name)

Answers (1)

dpb
dpb on 28 Sep 2024
Edited: dpb on 30 Sep 2024
"... pictures in the table to be saved in their pictural form?"
An image when load in memory is just an array (2 or 3D) and so writing a table will output the values as numeric; not as embedding into Excel as the image.
Somebody else asked just the other day about embedding an image in a cell in Excel; <I posted code there that seemed to work using ActiveX>; there is not any builtin function in MATLAB to do such...
The following is a totally untested rearrangement of your code above using one array for the three folders and using the above example code to place an image in a cell in a Lx3 array in the first sheet of a given workbook...good luck.
FQN=fullfile(pwd,'pic.xlsx'); % COM requires a fully-qualified name to open Excel file
excel=actxserver('Excel.Application'); % open the connection
wbk=excel.Workbooks.Open(FQN); % and the workbook
wksht=wbk.ActiveSheet; % and a reference to active sheet
d=[dir('**/NUSCH*AP/**/*.jpg'); % get the list of image files
dir('**/NUSCH*JS/**/*.jpg');
dir('**/NUSCH*LZ/**/*.jpg')];
N=numel(d); % how many are there
cols=3; % desired number columns
L=fix(N/cols); % how many rows needed
R=rem(N,cols); % how many left over after L rows
n=0;
for c=1:cols
for r=1:L+(c<=R) % write L rows plus 1 if remainder in column
cell=sprintf('%c%d',char('@')+c,r); % cell address for row, col
wksht.Range(cell).Select
n=n+1; % nth image to write
range.InsertPictureInCell(fullfile(d(n).folder,d(n).name)); % insert the image in the cell
end
end
excel.ActiveWorkbook.Save % write the updated file
excel.ActiveWorkbook.Close(0) % and close the file
excel.Quit; delete(excel);clear excel % clean up...
  7 Comments
Dominik Stolfa
Dominik Stolfa on 5 Oct 2024
Thanks. I am sorry but there is still some problem:
Unable to resolve the name 'range.InsertPictureInCell'.
Error in untitled (line 18)
range.InsertPictureInCell(fullfile(d(n).folder,d(n).name)); % insert the image in the cell
dpb
dpb on 5 Oct 2024
Another poster indicated same issue, but it works here -- I could not find documentation on the method and have no explanation other than differences in Excel versions...do you have the 365 subscription or are you using an older release of the standalone Office suite?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!