Trouble maintaining number format when converted to report

8 visualizaciones (últimos 30 días)
Floyd
Floyd el 26 de Dic. de 2023
Comentada: Floyd el 2 de En. de 2024
When a table is added to a report (after being imported from another source such as csv) the numbers that appear on the report are either incorrect or have changed formats s.t. they don't fit on the table properly. (No longer bank, f, g, long, short, etc.)
User can select specifics from the file in question and after deciding which row(s) to report on, and a report is generated.
Any suggestions on keeping the numbers the same? The Numbers are in the correct format when looking at them after the table is initially imported-before added to the report. It's only once they're added to the report that there is issues.
%% Variable/Item Selection For Report
format bank
fileChoice=readtable(uget);% converts and uses row 1 as variable names
cf=table2cell(fileChoice); % converts into cells for comparisons
% User selection could be either string or numeric
%% Report Generator
% First few lines setup the libraries required to gen. report and names it
format bank
% I know that having a second 'format bank' here does nothing, But I was still hoping
makeDOMCompilable();
import mlreportgen.report.*
import mlreportgen.dom.*
import mlreportgen.utils.*
R=Report("rptName",'pdf');
open(R);
R.Layout.Landscape=true;
pm=PageMargins();
pm.Left="0.1in";
pm.Right="0.1in";
pm.Bottom="0.1in";
pm.Top="0.1in";
% Title Page Setup
tp=TitlePage();
tp.Title="Testing";
tp.Subtitle="1 2 3";
tp.Author="Duck Duck";
tp.Publisher="Goose";
% Table setup
tb=Table(results);
tb.Border="solid";
slicer = mlreportgen.utils.TableSlicer("Table",tb,"MaxCols",nC);
tb.TableEntriesHAlign="center";tb.TableEntriesVAlign="middle";
tb.TableEntriesStyle=[tb.TableEntriesStyle {FontFamily('Times'),FontSize('6pt')}];
add(R,tp);add(R,tb);clc;close(R);rptview(R)

Respuestas (2)

Hassaan
Hassaan el 27 de Dic. de 2023
% Sample data - replace this with actual data
data = [0.01 0.888 1.0 40.151 39.1244;
0.02 0.444 2.0 35.123 34.5678];
% Convert numeric data to strings with consistent formatting
formattedData = arrayfun(@(x) sprintf('%.4f', x), data, 'UniformOutput', false);
% Initialize the report
import mlreportgen.report.*
import mlreportgen.dom.*
% Setup for a PDF report
report = Report('myReport','pdf');
% Add a title page
tp = TitlePage;
tp.Title = 'My Report Title';
tp.Author = 'Author Name';
report.add(tp);
% Add a table of contents
toc = TableOfContents;
report.add(toc);
% Add a chapter
chapter = Chapter;
chapter.Title = 'Data Analysis';
% Create a table and add it to the chapter
table = Table(formattedData);
table.Style = [table.Style {Border('solid', 'black', '3px')}];
table.TableEntriesHAlign = 'center';
table.TableEntriesVAlign = 'middle';
table.TableEntriesStyle = {TableEntryStyle({FontFamily('Times'), FontSize('9pt')})};
% Add the table to the chapter
chapter.add(table);
% Add the chapter to the report
report.add(chapter);
% Close the report (this generates the report)
close(report);
% Open the report for viewing
rptview(report);
Replace 'myReport' with the desired filename for your report, and customize the title, author, and other properties as needed. The data and its formatting (sprintf('%.4f', x)) should also be adjusted according to your specific requirements.
Remember to run this code in MATLAB where the Report Generator and DOM API are installed and available.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
  1 comentario
Floyd
Floyd el 28 de Dic. de 2023
When I try this, I end up with the error:
"Subscripting a table using linear indexing (onesubscript) or multidimensional indexing (three or more subscripts) is not supported. Use a row subscript and a variable subscript."
I'm not sure exactly what the error means, but I'm wondering if your solution didn't try to convert strings and caused it. Will your solution work since the array has both numerical AND string values or do you think that running some kind of loop to check each entry s.t. it only affects numerics would be necessary? I've never used that function before.

Iniciar sesión para comentar.


Hassaan
Hassaan el 28 de Dic. de 2023
Editada: Hassaan el 28 de Dic. de 2023
@Floyd Try this and let me know. Thank you.
% Sample data - replace this with actual data
data = [0.01 0.888 1.0 40.151 39.1244;
0.02 0.444 2.0 35.123 34.5678];
% Convert the numeric array to a MATLAB table with variable names
varNames = {'Var1', 'Var2', 'Var3', 'Var4', 'Var5'};
dataTable = array2table(data, 'VariableNames', varNames);
% Convert all numeric entries in the table to formatted strings
for i = 1:width(dataTable)
dataTable.(varNames{i}) = cellstr(num2str(dataTable{:,i}, '%.4f'));
end
% Initialize the report
import mlreportgen.report.*
import mlreportgen.dom.*
% Setup for a PDF report
report = Report('myReport','pdf');
% Add a title page
tp = TitlePage;
tp.Title = 'My Report Title';
tp.Author = 'Author Name';
report.add(tp);
% Add a table of contents
toc = TableOfContents;
report.add(toc);
% Add a chapter
chapter = Chapter;
chapter.Title = 'Data Analysis';
% Create a table and add it to the chapter
% Convert the formatted data table to cell array for Report Generator
formattedDataCell = table2cell(dataTable);
table = Table(formattedDataCell);
table.Style = [table.Style {Border('solid', 'black', '3px')}];
table.TableEntriesHAlign = 'center';
table.TableEntriesVAlign = 'middle';
table.TableEntriesStyle = {TableEntryStyle({FontFamily('Times'), FontSize('9pt')})};
% Add the table to the chapter
chapter.add(table);
% Add the chapter to the report
report.add(chapter);
% Close the report (this generates the report)
close(report);
% Open the report for viewing
rptview(report);
This code will:
  1. Convert your numeric data to a table with strings formatted to four decimal places.
  2. Initialize and generate a PDF report with the MATLAB Report Generator.
  3. Include a title page, table of contents, and a chapter that contains the data table.
  4. Format the table with borders, center alignment, and a specified font style and size.
Replace 'myReport' with the desired filename for your report, and customize the title, author, and other properties as needed.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
  1 comentario
Floyd
Floyd el 2 de En. de 2024
No effect.
Keep in mind that my 'data' values are both numeric and alphabetic.
I think this is why these functions aren't working, and by not working I mean that they change the formats BEFORE the report generation, but have no effect on the final report.

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by