Report Generator figures: List of figures not working

6 次查看(过去 30 天)
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

采纳的回答

Madheswaran
Madheswaran 2024-9-4
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!

更多回答(1 个)

ENRICO
ENRICO 2024-9-6
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 个评论
Madheswaran
Madheswaran 2024-9-9
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

请先登录,再进行评论。

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by