How to export 500 images in one file

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)

Risposte (1)

dpb
dpb il 28 Set 2024
Modificato: dpb il 30 Set 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 Commenti

Thank you for your response. Unfortunatelly I would like to be able to run my code on any complete instalation of Matlab (and some common program like Word, Adobe or Excel) without user having to install anything else. Is there a way to combine several pictures into one .pdf in MATLAB or to change representation of data in table into graphical? Like with imshow()?
dpb
dpb il 29 Set 2024
Modificato: dpb il 29 Set 2024
If the user has Excel, nothing else is needed...if they don't, you can't put them into a Word or Excel document anyway.
I suppose you could alternatively exportgraphics to pdf although I've no experience. I do not know whether you can make an array of images as your Q? requested, however.
Report Generator has the potential to generate pdf.
Aha.
I understand. There is some error though:
Unable to resolve the name 'wksht.Range'.
Error in untitled (line 15)
wksht.Range(cell).Select
dpb
dpb il 30 Set 2024
Modificato: dpb il 30 Set 2024
My bad; a line got left out in the copying over and rearrngement...add
wksht=wbk.ActiveSheet;
after the line that opens the workbook.
As I coached in the Answer in the other thread, I recommend you set a breakpoint and test each line first and be prepared to restart Excel and close hung processes with task manager while getting things working...
BTW, to select any given sheet, use
excel.Worksheets.Item(sheetNameOrNumber).Activate
where sheetNameOrNumber is either a number (1, 2, ...) or the sheet name as a char() string variable; MATLAB cellstr() won't work; I think I recall a MATLAB string variable may, but the char() will always.
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
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?

Accedi per commentare.

Richiesto:

il 28 Set 2024

Commentato:

dpb
il 5 Ott 2024

Community Treasure Hunt

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

Start Hunting!

Translated by