How to aappend Image with caption in Paragraph using mlreportgen while generating report?

11 views (last 30 days)
I want to add image with caption in the paragraph. Caption property is available in FormalImage but, can not append that in paragraph. For appending image in paragraph we need to use Image with does not have caption feature.
Please help me with this. Thank you.

Answers (1)

Kausthub
Kausthub on 12 Feb 2024
Hi Srushti,
I believe that you would like to add an image with a caption in the paragraph. Yes, you are right about "Image" not having the option to add the caption but you could split the paragraph into parts and add the "FormalImage" in between them.
A snippet for the same:
import mlreportgen.report.*
import mlreportgen.dom.*;
% Create the report
rpt = mlreportgen.report.Report('output','docx');
% Create the chapter
chapter = mlreportgen.report.Chapter();
chapter.Title = 'Image with Caption within a Paragraph';
% Create the paragraph
p = Paragraph(['Lorem ipsum dolor sit amet, consectetur adipiscing elit,' ...
' sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' ...
' Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris' ...
' nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in' ...
' reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla' ...
' pariatur. Excepteur sint occaecat cupidatat non proident, sunt in' ...
' culpa qui officia deserunt mollit anim id est laborum.']);
% Add the paragraph
append(chapter,p);
image = mlreportgen.report.FormalImage();
image.Image = which('ngc6543a.jpg');
image.Caption = 'Cat''s Eye Nebula or NGC 6543';
image.Height = '5in';
% Add the image
add(chapter,image);
% Add the second paragraph
append(chapter,p);
% Add the chapter
add(rpt,chapter);
rptview(rpt);
Also, I have added the screenshot of the created document for your reference.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!