Trouble maintaining number format when converted to report
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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)
0 Kommentare
Antworten (2)
Hassaan
am 27 Dez. 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.
Hassaan
am 28 Dez. 2023
Bearbeitet: Hassaan
am 28 Dez. 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:
- Convert your numeric data to a table with strings formatted to four decimal places.
- Initialize and generate a PDF report with the MATLAB Report Generator.
- Include a title page, table of contents, and a chapter that contains the data table.
- 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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!