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

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

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

@Umar Thannk you so much fro your answer, it worked really well. Just allow me inquire one more,
Suppose I have a string already as shown below. Suppose the output correspond to the month of January and I want to access one of the elements of the sring and append it in the paragraph. Such that the paragraph now read "The wind power output of the area is 2 in January" just for example. How about that, how can i go about this?
A=string('January','Feebruary','March','April','May')

Hi @okoth ochola ,

You can access the specific month from your string array and concatenate it with the power output. Here’s how you can modify your code:

First, ensure your string array is defined correctly. You can use the following code to create the string array:

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

Next, you can access the month you want (for example, January, which is the first element) and concatenate it with the power output. Here’s the modified line of code:

month = A(1); % Accessing the first element, which is 'January'
para = Paragraph(['The wind power output of the area is ', 
num2str(Power), ' in ', month]);

This line constructs the paragraph dynamically, ensuring it reads, "The wind power output of the area is 2 in January" (assuming Power is 2). The num2str(Power) function converts the numerical value to a string, allowing for seamless concatenation with the month string. Finally, add this paragraph to your section as before, and your report will reflect the updated content accurately.

Hope this helps.

@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]);

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

Asked:

on 20 Oct 2024

Commented:

on 22 Oct 2024

Community Treasure Hunt

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

Start Hunting!