Generating multiple page content using Report generator!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1744196/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1744201/image.png)
23 commentaires
Réponses (5)
5 commentaires
Hi @Divyam,
Never mind, I did go through the comments and saw the attached zip file. I wished could see it earlier. I do really apologize for it since causing delay in answering @Srujeeth B. You did a good job implementing it Howe I did execute your code and it had minor errors, please see attached.
![](/matlabcentral/answers/uploaded_files/1747011/IMG_7336.jpeg)
Afterwards, I did modify your code to fix the errors. The error message Array indices must be positive integers or logical values was indicating that the index i was not correctly updating during the loop iteration. Before entering the loop, make sure that the index variable i is properly initialized to a valid value. For example, you can set i = 1 before the loop starts. Inside the loop, ensure that i is updated correctly to iterate over the elements of Data.S. You can increment i at the end of each iteration to move to the next set of data. Verify that the index i stays within the bounds of the Data.S cell array to prevent index out-of-bounds errors. You can use a conditional check to ensure that i does not exceed the number of elements in Data.S.
Here is the updated code,
import mlreportgen.report.*
import mlreportgen.dom.*;
Data.S{1} = [1 2 3];
Data.S{2} = [8 16 24];
Data.S{3} = [6 12 18];
TestDel = Document('TDeleDoc', 'docx', '/MATLAB Drive/TestToDelete.dotx');
open(TestDel);
i = 1; % Initialize the index variable
while ~strcmp(TestDel.CurrentHoleId, '#end#')
disp(TestDel.CurrentHoleId);
switch TestDel.CurrentHoleId
case '#start#'
moveToNextHole(TestDel);
continue;
case 'Test1'
append(TestDel, 12);
case 'Test2'
append(TestDel, Data.S{i}(2));
case 'Test3'
append(TestDel, Data.S{i}(3));
otherwise
% Check and process the data type before appending
holeData = TemplateHole(TestDel.CurrentHoleId);
if ischar(holeData)
append(TestDel, holeData);
else
% Handle other data types accordingly
% For example, convert to char or handle differently
end
end
i = i + 1; % Update the index variable
moveToNextHole(TestDel);
end
close(TestDel);
Please see attached.
![](/matlabcentral/answers/uploaded_files/1747031/IMG_7337.jpeg)
Hope, this will make @Srujeeth B happy. It was a simple problem and now it is resolved.
1 commentaire
3 commentaires
Hi @Srujeeth B ,
I am not giving up till you are satisfied. So, I have modified the code again and now it achieves the desired outcome of populating holes sequentially for different i values in a document. By using a loop from i=1 to i=3, the code inserts the data sets into the document with appropriate page breaks which ensures that each data set is displayed on a separate page as required. The use of append functions and PageBreak() helps in organizing the content for each i value systematically. Overall, it also aligns within your specified requirements such as presenting the data sets in a single document with distinct pages for each i value and successfully generates pages with content for different dataset values. Here is modified code,
%% Create the document
import mlreportgen.report.*
import mlreportgen.dom.*
TestDel = Document('TDeleDoc','docx','/MATLAB Drive/TestToDelete.dotx');
%% Load your template
Data.S{1} = [1 2 3];
Data.S{2} = [8 16 24];
Data.S{3} = [6 12 18];
%% Define content for holes
Content = {'Content for Hole AA', 'Content for Hole BB', 'Content for Hole CC'};
numHoles = numel(Content);
%% Open the document outside the loop
open(TestDel);
%% Loop through different pages and populate holes sequentially
for i = 1:3
% Add a page break before displaying data for i > 1
if i > 1
append(TestDel, PageBreak());
end
% Display the data for the corresponding i value
append(TestDel, sprintf('Data Set for i=%d: %s\n', i, mat2str(Data.S{i})));
end
%% Close the report
close(TestDel);
Please see attached,
![](/matlabcentral/answers/uploaded_files/1747386/IMG_7343.jpeg)
Hope, the modified code finally meets your requirements. Also, it resolved your error problem as mentioned earlier, “ I tried using pageBreak() statement at the end of the page. And that being the last hole in the report. I receive the following error ''Error using mlreportgen.dom.Document/append Unable to append to #end# hole''. I did try adding additional hole in the template after the line of page break, buit still it loads my first holes content and then throws the same error.” Please let me know if you still have any further questions.
Hi @ Srujeeth B,
In the modified code snippet above, I have replaced the direct insertion of content with the append function to ensure a consistent approach. I have included the switch statement to populate the holes AA, BB, and CC with the respective content based on the loop iteration. This modification ensures that the holes are filled sequentially with the specified content while maintaining the use of the append function throughout the process. Here is modified code snippet,
%% Create the document
import mlreportgen.report.*
import mlreportgen.dom.*
TestDel = Document('TDeleDoc','docx','/MATLAB Drive/TestToDelete.dotx');
%% Load your template
Data.S{1} = [1 2 3];
Data.S{2} = [8 16 24];
Data.S{3} = [6 12 18];
%% Define content for holes
Content = {'Content for Hole AA', 'Content for Hole BB', 'Content for Hole CC'}; numHoles = numel(Content);
%% Open the document outside the loop
open(TestDel);
%% Loop through different pages and populate holes sequentially
for i = 1:3
% Add a page break before displaying data for i > 1
if i > 1
append(TestDel, PageBreak());
end
% Populate the holes with corresponding content
switch i
case 1
append(TestDel, Content{1});
case 2
append(TestDel, Content{2});
case 3
append(TestDel, Content{3});
end
% Display the data for the corresponding i value
append(TestDel, sprintf('Data Set for i=%d: %s\n', i, mat2str(Data.S{i})));
end
%% Close the report
close(TestDel);
You can effectively modify the code to use append exclusively and populate the designated holes with the specified content as required. Hope, this helps now.
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!