Effacer les filtres
Effacer les filtres

Generating multiple page content using Report generator!

171 vues (au cours des 30 derniers jours)
Srujeeth B
Srujeeth B le 31 Juil 2024 à 9:23
Commenté : Divyam le 6 Août 2024 à 10:03
I am using report generator to generate a document in Matlab. The document is generated by form based generation process, where my template has a number of holes, at a well defined positions. I do not have any sections or chapter or paragraphs to append directly into.
The problem is in using the template and to genearate different pages. 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.
To be more concise, I simply want to reuse the template(consititng of signle page with multiple holes) and loop it several times for the different data, so that I can capture all the single page document outputs in one single document.
Note: Above picture illustrates how the page generating function (defined by 'MesaPointAnalys') terminates.
Note: Above picture illustrates how the loop is performed to call for assignement for different page. The MesaPointAnalys is a page generating function and handles different data based on the page numbers.
  23 commentaires
Walter Roberson
Walter Roberson le 4 Août 2024 à 19:08
I am staying out of the discussion because I do not know anything about Report Generator.
I am merely lightly editting postings in order to format code properly.
Umar
Umar le 5 Août 2024 à 1:16
Hi @ Srujeeth B,
This will be my last tip for troubleshooting because these were all the necessary steps for troubleshooting errors in your code. So, check for any hidden characters or formatting inconsistencies that could be affecting the appending process. By systematically verifying each step and ensuring consistency between the template and code, you can troubleshoot and resolve the 'Unable to append to #end# hole' error effectively. If issue still persists then I will email one of the moderators again to expedite this process.

Connectez-vous pour commenter.

Réponses (5)

Divyam
Divyam le 5 Août 2024 à 10:14
Modifié(e) : Divyam le 6 Août 2024 à 4:14
I went through the "test.zip" files provided in the comments. Indexing through holes using a 'for' loop will often lead to the "Unable to append to #end# hole'' error as you might encounter the last hole by using the 'moveToNextHole' function.
A 'while' loop can be used instead where you are iterating until you reach the '#end#' hole. You can display the 'CurrentHoleId' in the loop and create a switch case scenario for each 'CurrentHoleId' to append the relevant data to the respective hole.
clearvars
clc
%% Create the document
import mlreportgen.report.*
import mlreportgen.dom.*;
%% Load your template and Data
Data.S{1}=[1 2 3];
Data.S{2}=[8 16 24];
Data.S{3}=[6 12 18];
TestDel = Document('TDeleDoc','docx','<Add File Path Here>');
%% Parse through the template and append data to the holes
open(TestDel);
% Iterate through all the holes until you reach the last hole
while ~strcmp(TestDel.CurrentHoleId,'#end#')
% Display 'CurrentHoleId' to get an idea of which hole to append the
% data
disp(TestDel.CurrentHoleId);
switch TestDel.CurrentHoleId
% Move to the next hole when encountering the '#start#' hole
case '#start#'
moveToNextHole(TestDel);
continue;
% Append data to the three holes in the template
case 'Test1'
append(TestDel,12);
case 'Test2'
append(TestDel,Data.S{i}(2));
case 'Test3'
append(TestDel,Data.S{i}(3));
% If you arent appending any data to an existing hole, append a new
% hole in the existing hole so that Report Generator does not
% discard this hole
% Note: Remove this case if you dont intend to further use an
% existing hole in which you arent appending any data
otherwise
append(TestDel,TemplateHole(TestDel.CurrentHoleId));
end
moveToNextHole(TestDel);
end
close(TestDel);
  5 commentaires
Srujeeth B
Srujeeth B le 6 Août 2024 à 9:39
Hi! Divyam
I am sorry if the results are driving some where out of track.
Below figure explains what I am expecting. The coloured text is for explaination. Text in black is should be the the report generator output file I am expecting.
One other thing to note that, it is possible to generate holes on all the pages (that is 3 holes in each page) and then things will work. Well that solution will not help as it will not work if I am have like 100 -200 pages dataset to be filled.
Divyam
Divyam le 6 Août 2024 à 10:03
Hi @Srujeeth B, by using the code I provided and editing the document template, the expected functionality will be achieved with minor tweaks to the switch case statements. If you wish to create a large report for a large dataset, then I would suggest using a table rather than editing a document template.
For more information on how you can incorporate tables in your workflow using the MATLAB Report Generator, refer to this documentation: https://www.mathworks.com/help/rptgen/ug/create_a_table_from_cell_array.html

Connectez-vous pour commenter.


Umar
Umar le 5 Août 2024 à 11:30
Modifié(e) : Umar le 5 Août 2024 à 11:34

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.

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.

Hope, this will make @Srujeeth B happy. It was a simple problem and now it is resolved.

  1 commentaire
Srujeeth B
Srujeeth B le 5 Août 2024 à 12:17
Modifié(e) : Srujeeth B le 5 Août 2024 à 12:20
Well! Thanks for refining it @Umar.
Though it does not generate any errors. It is just overwriting the previous content (as mentioned above). What I meant is that i value get iterated and the final data set is only presented in the report. I need to have the results of data set for i=1 (on one page), i=2 (on next page) and i=3 (on last page). So all the results are together included in the same document.
So, issues still exits, I cannot generate pages with content for different data set values !!

Connectez-vous pour commenter.


Umar
Umar le 5 Août 2024 à 16:55
Hi @ Srujeeth B,
To address your query regarding, “ What I meant is that i value get iterated and the final data set is only presented in the report. I need to have the results of data set for i=1 (on one page), i=2 (on next page) and i=3 (on last page). So all the results are together included in the same document. ”
I made modifications to the code and it effectively organizes the results in the report as follows:Results for 'i=1' are displayed on one page, Results for 'i=2' are presented on the next page and Results for 'i=3' are shown on the last page. Also, all results are consolidated within the same document, with each 'i' value's data set and corresponding details clearly separated by page breaks. Please see attached.
  3 commentaires
Umar
Umar le 5 Août 2024 à 19:55
Hi @Srujeeth B ,
Thank you for sharing your feedback regarding the hole population process. I appreciate your detailed explanation of the challenges you are facing with nearly 30 holes to be populated, some located on the figure and others side by side.
I understand that using append to populate the holes one after the other does not align with your application requirements. Your approach of sequentially feeding content into each hole using append and moveToNextHole seems to be more suitable for your specific needs.
I will take this into consideration and explore alternative solutions that may better accommodate your application requirements. Please feel free to provide any additional details or specifications that could help us tailor our approach to better meet your needs.
Thank you once again for sharing your insights. We are committed to finding a solution that works best for you.
Srujeeth B
Srujeeth B le 5 Août 2024 à 20:04
Modifié(e) : Srujeeth B le 5 Août 2024 à 20:06
Thank you @Umar for trying to help in resolving the problem. Appreciate your efforts!!
I appolize in case if you have missed to notice it earlier in my shared code comments:
''Here is the short version of my code that stimulates the same error I encounter..."
I don't see anything as of now. Once again thankyou for your polite answers and efforts!

Connectez-vous pour commenter.


Umar
Umar le 5 Août 2024 à 21:07
Modifié(e) : Umar le 5 Août 2024 à 21:11

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,

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.

  1 commentaire
Srujeeth B
Srujeeth B le 5 Août 2024 à 22:40
hi! @Umar
Suggested code is similar as before, it does use append only. Does not use the holes specified in the template.

Connectez-vous pour commenter.


Umar
Umar le 5 Août 2024 à 23:50

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.

  1 commentaire
Srujeeth B
Srujeeth B le 6 Août 2024 à 9:48
This is what is happening:
I am expecting result as:
One other thing to note that, it is possible to generate holes on all the pages (that is 3 holes in each page) and then things will work. Well that solution will not help as it will not work if I am have like 100 -200 pages dataset to be filled.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by