Can we tell Matlab to write specific dynamic information in the generated report?

3 views (last 30 days)
Hi, I have an inquiry to make about report generation in matlab. Suppose I have a simple program as shown in the first code. Now I would like to pass the result of the output of the function named "power" in the program to the report within the paragraph explaining that result as illustrated in the report generation code. Such that, if the output power is 2, the paragraph under "Wind power output" in the final report would read "The wind power output of the area is 2". That is, the program inserts the value of "power" in the carli brackets by itself. Is this possible? Any ideas and clues would be highly appreciated. Thank you.
b=input('enter the wind speed\n')
Powere=0.5*1.225*b^3;
%report generation code
makeDOMCompilable();
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('Wind Energy Assessment','docx');
tp = TitlePage;
tp.Title = 'WIND POWER POENTIAL REPORT';
tp.Author = 'GESC';
add(rpt,tp);
add(rpt,TableOfContents);
ch1 = Chapter;
ch1.Title = 'INTRODUCTION';
sec1 = Section;
sec1.Title = 'Wind power output';
para = Paragraph([' The wind power output of the area is {the program inserts the value of "power"}']);
add(sec1,para)
add(ch1,sec1)
add(rpt,ch1)
delete(gcf)
close(rpt)
rptview(rpt)

Accepted Answer

Umar
Umar on 20 Oct 2024

Hi @okoth ochola,

You mentioned, “the paragraph under "Wind power output" in the final report would read "The wind power output of the area is 2". That is, the program inserts the value of "power" in the carli brackets by itself. Is this possible? Any ideas and clues would be highly appreciated.”

The code provided below demonstrates a straightforward approach to calculating wind power output based on user input and subsequently generating a report that includes this calculated value. Let me break down the process step-by-step to clarify how the dynamic insertion of the output into the report is achieved. The first part of code captures user input for wind speed and calculates the wind power output using the formula:

% Input wind speed
b = input('Enter the wind speed (m/s):\n');
% Calculate wind power output
Power = 0.5 * 1.225 * b^3;

Here, b represents the wind speed in meters per second, and the formula calculates the wind power output (Power) based on the cubic relationship with wind speed. The constant 0.5 * 1.225 represents the air density and the efficiency factor.

The next section of code utilizes the MATLAB Report Generator to create a structured report. The relevant part of the code for inserting the calculated power output into the report is as follows:

import mlreportgen.report.* 
import mlreportgen.dom.* 
% Create a new report
rpt = Report('Wind Energy Assessment', 'docx'); 
% Add title page
tp = TitlePage; 
tp.Title = 'WIND POWER POTENTIAL REPORT'; 
tp.Author = 'GESC'; 
add(rpt, tp); 
% Add table of contents
add(rpt, TableOfContents); 
% Create a chapter
ch1 = Chapter; 
ch1.Title = 'INTRODUCTION'; 
% Create a section for wind power output
sec1 = Section; 
sec1.Title = 'Wind Power Output'; 
% Create a paragraph with dynamic content
para = Paragraph(['The wind power output of the area is ', num2str(Power)]); 
add(sec1, para); 
% Add section to chapter and chapter to report
add(ch1, sec1);
add(rpt, ch1);
% Close and view the report
delete(gcf); 
close(rpt);
rptview(rpt);

The key line that dynamically inserts the calculated power output into the report is:

para = Paragraph(['The wind power output of the area is ', num2str(Power)]);

In this line:

  • num2str(Power) converts the numerical value of Power into a string format, which is essential for concatenation with the text.
  • The square brackets [] are used to concatenate the static text with the dynamic value, creating a complete sentence that reads, for example, "The wind power output of the area is 2".

After constructing the paragraph, it is added to the section, which is then included in the chapter, and finally, the chapter is added to the report. The report is then closed and displayed using rptview(rpt).

This approach not only automates the report generation process but also ensures that the output is accurately reflected in the text, fulfilling the user's requirement.

Hope this helps.

If you have any further questions or need additional clarification on specific parts of the code, feel free to ask!

  4 Comments
okoth ochola
okoth ochola on 21 Oct 2024
@Umar I tried that earlier but it did not work. I am geting the following error, just like earlier
Error using mlreportgen.dom.Paragraph
No constructor 'mlreportgen.dom.Paragraph' with matching signature found.
Error in reportrial (line 25)
para = Paragraph(['The wind power output of the area is ', num2str(Power), 'W/m^2' , num2str(Power), 'in', month]);
Umar
Umar on 22 Oct 2024

Hi okoth ochola,

I reviewed your comments and errors you encountered in code. To resolve the issue you are facing with the mlreportgen.dom.Paragraph constructor, ensure that you are correctly importing the necessary package and that your syntax is accurate. Here is a refined approach to your code: First, define your string array as follows:

A = ["January", "February", "March", "April", "May"];

Next, access the desired month and concatenate it with the power output. Here’s the corrected line of code:

month = A(1); % Accessing 'January'
para = mlreportgen.dom.Paragraph(['The wind power output of the 
area is ',num2str(Power), ' W/m^2 in ', month]);

Make sure to include the mlreportgen.dom package at the beginning of your script:

   import mlreportgen.dom.*;

This should eliminate the error and allow you to create the paragraph correctly. The output will read, "The wind power output of the area is 2 W/m^2 in January" (assuming Power is 2). If you continue to experience issues, please verify that the mlreportgen toolbox is installed and properly configured in your MATLAB environment.

Please let me know if this helped resolve the problem.

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!