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

조회 수: 4 (최근 30일)
Karlie Hornberger
Karlie Hornberger 2017년 6월 1일
댓글: Karlie Hornberger 2017년 6월 2일
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?

채택된 답변

Paul Kinnucan
Paul Kinnucan 2017년 6월 2일
편집: Paul Kinnucan 2017년 6월 2일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Report Generator Task Examples에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by