Report Generator figures: List of figures not working

5 views (last 30 days)
I'm trying to generate automatically a figure report from currently open figures list. I'm using code reported below. My issue is, I'm not able to create a coherent list of figures and a summary of illlustrations to link to single images. It looks my code is not too different from examples, so I don't find reasons for not working... Please help! (Sorry for dirty coding but is a WIP):
function ReportFigures(ReportName,fileType)
if nargin<2
fileType='pdf';
end
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report(ReportName,fileType);
open(rpt);
toc = TableOfContents();
append(rpt,toc);
lof = ListOfFigures();
lof.Title = "List of Figures";
append(rpt,lof);
ch = Chapter("Images");
p = Paragraph('Pictures from script');
p.Style = {Color('red'),FontFamily('Arial'),FontSize('18pt')};
p.Style = {OuterMargin('0.5in','0in','0in','12pt')};
p.HAlign = 'center';
% ch.Style = {Color('red'),FontFamily('Arial'),FontSize('18pt')};
append(ch,p);
h= findall(groot,'Type','figure')
for k=1:length(h)
fileImageName=['Tmp' num2str(k) '.png'];
saveas(h(k),fileImageName);
%image1 = FormalImage(which(get(h(k),'Name')))
image1(k)=Image(fileImageName);
%image1=Image(getSnapshotImage(h(k),rpt));
%image1.ScaleToFit = true;
%image1(k).Caption = get(h(k),'Name');
% image1
image1(k).Height = "3in";
image1(k).Width = "5in";
% image1.Title=get(h(k),'Name');
append(p,image1(k));
% surf(peaks);
% fig = Figure();
% fig.Snapshot.Caption = '3-D shaded surface plot';
% fig.Snapshot.Height = '5in';
% fig=h(k);
% fig.Snapshot.Caption = get(h(k),'Name');
% fig.Snapshot.Height = "5in";
% append(ch,fig);
end
append(rpt,ch);
%delete(gcf);
close(rpt);
rptview(rpt);
end

Accepted Answer

Madheswaran
Madheswaran on 4 Sep 2024
Hello,
It seems you are trying to use the 'Caption' property with the ‘mlreportgen.dom.Image’ class, which doesn't support this property. To generate a list of figures, I recommend using the ‘mlreportgen.report.FormalImage’ class which allows you to add captions. Additionally, append the images directly to the 'Chapter' rather than to a 'Paragraph'.
Here's the revised code that should meet your requirements:
% ... existing code
h = findall(groot, 'Type', 'figure');
for k = 1:length(h)
fileImageName = ['Tmp' num2str(k) '.png'];
saveas(h(k), fileImageName);
% Create a formal image
img = FormalImage(fileImageName);
img.Caption = get(h(k), 'Name');
img.Height = '3in';
img.Width = '5in';
% Append the image directly to the chapter
append(ch, img);
end
append(rpt, ch);
% ... existing code
The modified code will help you to add the list of figures section as shown in the below image:
For further details, please refer to the following MATLAB documentations:
  1. FormalImage - https://mathworks.com/help/rptgen/ug/mlreportgen.report.formalimage-class.html
  2. List of Figures - https://mathworks.com/help/rptgen/ug/mlreportgen.report.listoffigures-class.html
Hope this helps!

More Answers (1)

ENRICO
ENRICO on 6 Sep 2024
Ok, as you imagine from my messy codepiece I had some troubles with FormalImage initially I guessed to be due to the call, but in the end were related to picture name having unallowed characters.
Actually your corrections works fine. But still a doubt left. i see piicture reporting works anyway with Image and figures, but are not correctly listed on figures list summary! That was my point, and it sund strange...
anyway thank you much for your answer!
  1 Comment
Madheswaran
Madheswaran on 9 Sep 2024
I am assuming this post is a comment to my answer.
Glad my answer helped you. To answer your doubt, to generate the list of figures, you should insert the image using mlreportgen.report.FormalImage or mlreportgen.report.Figure objects with captions as mentioned in the documentation: https://mathworks.com/help/rptgen/ug/mlreportgen.report.listoffigures-class.html

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!