MATLAB: How to switch between landscape and portrait page orientation when creating a Word report using MATLAB Report Generator R2019a

MATLAB Report Generator

I like to create a Word report (docx) and for that I need to switch between landscape and portrait multiple times. What is the right way to achieve this?

Best Answer

Each time you want to change the page layout in the document, you need to create a new DOCXPageLayout object, set the orientation, and append it to the document object. If you are switching between portrait and landscape pages multiple times in the document, one strategy is to create two page layout objects, one portrait and one landscape, and append clones of the objects to the document whenever you need to switch the orientation. For example:
import mlreportgen.dom.*
d = Document('exampleDoc', 'docx');
open(d);
% default page layout is portrait
portraitPLO = DOCXPageLayout;
portraitPageSize = portraitPLO.PageSize;
% define landscape layout
landscapePLO = DOCXPageLayout;
landscapePLO.PageSize.Orientation = "landscape";
landscapePLO.PageSize.Height = portraitPageSize.Width;
landscapePLO.PageSize.Width = portraitPageSize.Height;
table = append(d,magic(15));
table.Border = 'solid';
table.ColSep = 'solid';
table.RowSep = 'solid';
%switch to landscape
append(d, clone(landscapePLO));
table = append(d,magic(11));
table.Border = 'solid';
table.ColSep = 'solid';
table.RowSep = 'solid';
%switch to portrait
append(d, clone(portraitPLO));
table = append(d,magic(5));
table.Border = 'solid';
table.ColSep = 'solid';
table.RowSep = 'solid';
%switch to landscape again
append(d, clone(landscapePLO));
table = append(d,magic(8));
table.Border = 'solid';
table.ColSep = 'solid';
table.RowSep = 'solid';
close(d);
rptview(d);