Limiting export of digits in mlreport.gen.dom*?

4 vues (au cours des 30 derniers jours)
Karlie Hornberger
Karlie Hornberger le 1 Juin 2017
I'm using mlreport.gen.dom* to fill calculations into a word template with holes. For example:
hole1=1.535;
hole2=hole1*4000;
import mlreportgen.dom.*;
namethedoc='testing'
type = 'docx';
calltheform='must have a form to run this sample'
rpt = Document(namethedoc,type,calltheform);
open(rpt);
while(~strcmp(rpt.CurrentHoleId,'#end#'))
switch(rpt.CurrentHoleId)
case 'Hole1'
append(rpt,hole1);
case 'Hole2'
append(rpt,hole2);
end
moveToNextHole(rpt);
end
close(rpt);
docview('testing');
When running this on its own, none of the numbers round when they appear in Word (they report too many decimal points). To combat that, I added a separate rounding.m file for rounding and changed the cases to append(rpt,rounding(dx1)). If the number is small (between 0 and 5), I want several decimal points read out, otherwise, the nearest integer is fine. The function, given below, and several variants have not successfully put the rounded number into the document for the 0 to 5 range. It even takes given values, such as 1.535, and makes it into 1.534999999999999999.
function y=rounding(x)
clc
if x>0 && x<5
format short
y=round(x,5,'significant')
fprintf('%5.2f',y)
else
format short
y=round(x)
fprintf('%f',y)
end
end
I've tried with and without the fprintf and several other variations.
Any ideas about how I can limit the display of digits when copying from MATLAB to Word?

Réponse acceptée

Paul Kinnucan
Paul Kinnucan le 2 Juin 2017
Modifié(e) : Paul Kinnucan le 2 Juin 2017
Try this:
import mlreportgen.dom.*
d = Document('FloatingPoint', 'docx');
append(d, Paragraph(rounding(2.129999)));
append(d, Paragraph(rounding(6.5)));
close(d);
rptview(d);
function y=rounding(x)
if x>0 && x<5
format short
y=round(x,5,'significant');
y = sprintf('%5.2f',y);
else
format short
y=round(x);
y = sprintf('%0.0f',y);
end
end

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB Report Generator Task Examples dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by