How can I put images in a table using gui?

I need to create a table in matlab/gui where I have to put images in the first column of the table. Is that possible? I searched a lot for a code to help me but I couldn't find any.

 Accepted Answer

Adam Danz
Adam Danz on 7 Apr 2023
Edited: Adam Danz on 7 Apr 2023
Here's a demo that embeds images in cells of a uitable but as @Walter Roberson mentioned, the image size will be small, and the aspect ratio may change since all icons are the same size.
uistyle is used to create icons from the images and addStyle is used to place the images in the uitable cells.
% 1. Create a demo table of images
images = ["circle.jpg" "clock.jpg" "catherine.jpg" "hexagon.jpg", "laure.jpg", "pearswtx.jpg" "star.jpg" "triangle.jpg"];
T = table('Size',[numel(images),5], ...
'VariableTypes',["string" "string" "double" "double" "double"], ...
'VariableNames',["Image" "Format" "BitDepth" "Width" "Height"]);
T.Image = images(:);
for i = 1:numel(images)
info = imfinfo(T.Image(i));
T.Format(i) = info.Format;
T.BitDepth(i) = info.BitDepth;
T.Width(i) = info.Width;
T.Height(i) = info.Height;
end
% 2. Create the uitable and embed images in column 1.
fig = uifigure;
uit = uitable(fig,"Data",T, ...
'Units','normalized', ...
'Position',[.05 .05 .9 .9], ...
'FontSize', 16);
for i = 1:height(T)
imgStyle = uistyle("Icon",T.Image(i));
addStyle(uit, imgStyle, 'cell', [i,1])
end

16 Comments

That is so helpful! Thanks a lot
I have the following code to read images from a folder on my PC. Then,I want to save the images name an array. The output should be something like :
images=["pic1.jpg" "pic2.jpg"]
However, images array keeps updating. How can I fix it?
imds = imageDatastore('*.jpg')
fullFileNames = vertcat(imds.Files)
for k = 1 : length(fullFileNames)
[folder, baseFileNameNoExt, ext] = fileparts(fullFileNames{k});
baseFileNameWithExt = [ baseFileNameNoExt, ext]
images=strings(1);
images=[images,baseFileNameNoExt]
end
images(k, 1) = string(baseFileNameWithExt)
If you're using MATLAB R2020b or later, fileparts accepts cell array of character vectors so you can pull out the file.ext strings using,
imds = imageDatastore('*.jpg');
[~, name, ext] = fileparts(imds.Files);
images = string(strcat(name,ext));
Hello Sir @Adam Danz,
I used your demo to generate the following table. Now if I need to count how many "Khalal" appeared only when the Date type was Barhi. Which functions should I use? The answer should be 2.
Compute the sum of two column indices. The first index will look for "Barhi" in the DataType column and the scone index will look for "Khalal" in the MaturityStage column. It will look something like,
sum(T.DataType=="Barhi" & T.MaturityStage=="Khala");
Or you could use groupsummary to compute the number of all conditions.
Hello Mr @Adam Danz
I am trying to save the table as a pdf using the following code, but the pdf generated is empty:
exportapp(fig, 'info.pdf')
none of them are working :(
exportgraphics, saveas, and print do not capture UI components and will throw an error if the figure does not contain graphics that are capturable.
exportapp, on the other hand, should work. As evidence, attached is the pdf that was produced by exportapp in R2023a.
  1. Do you see an error message when attempting to run exportapp?
  2. When you say the PDF is empty, do you mean it's a white, blank page or the table is empty?
  3. Did you visually confirm that your figure isn't empty?
  4. Are you sure you're using the correct fig handle?
  5. Are you running exportapp directly after the figure is produced or are you giving the graphics system time to fully generate the figure?
  6. What release of MATLAB are you using?
I put exportapp in another section and it worked after running the previous section. But I have another question. If my uifigure contains three tables, how can I create a pdf with three pages, instead of one page (each table should be saved in a page.)
exportapp does not have an append option. You can combine PDF files into one file using other software.
Hello Mr @Adam Danz
Is there a way to save the uifigure as an excel file instead of pdf? I have a uifigure similar to what you have shared in test.pdf
@Adam - Is there a way to put multiple icons/images in a cell?
I have a bunch of modifiers that apply to a row of a table (think like a git interface where you have add/delete/modified/stale etc.). I'd like to add the relevant ones for each row. Using uistyle(Icon...) seems to only add one and overwrite (even if Alignment is left/right). uistyle(Interpreter="html") does not seem to render images (it shows the missing icon regardless of path management). The uistyle doc does say the <img> tag is unsupported.
d = addvars(array2table(magic(3)), strings(3,1), 'NewVariableNames', {'modifiers'}, 'Before', 1);
report_24 = ['file:' which('report_24.png')]; % on path; file:/ from https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels
edit_icon = 'edit_icon.png'; % pwd
d.modifiers(1) = ['<html><body><img src="' report_24 '"><img src="' edit_icon '"></body></html>'];
warningicon = which('warning_16.png'); % no file:
d.modifiers(3) = ['<img src="' warningicon '">'];
t = uitable(uifigure, Data=d);
addStyle(t, uistyle(Interpreter="html"), "column", 1)
The only thing I've been able to figure out is to manually combine the icons and concatenate the image but the combinatorics of that are kinda ugly.
It's been awhile, hope all is well!

Sign in to comment.

More Answers (2)

If you use traditional figures with traditional uitable() objects, then Yes, it is possible.
What you do in such cases is use a cell array of character vectors, and set each one to be HTML, such as
D{2,5} = '<HTMTL><IMG source="http://SOMEURL">'
However.... it is difficult to adjust the size of the cells. You can adjust column width, but column height is going to be automatically determined to be equivalent to one line in the text font (the scanner is just going to see text, not understanding that it is going to activate something.) So you can effectively only get fairly small images, on the order of 10-ish pixels high.
If you are using uifigure() with newer uitable() then there might be additional possibilities.

Categories

Community Treasure Hunt

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

Start Hunting!