How to specify number of decimals in table2word or writematrix
조회 수: 29 (최근 30일)
이전 댓글 표시
Is it possible to write a double-valued matrix/array to a Word (or comma-separated text file) table with a specified format, e.g. a fixed-point notation with a specified number of decimals?
By default, writematrix seems to use "longg (15 digits of precision)" (see Guillaume's comment in
).
Function table2word seems to produce a fixed-point notation with 6 decimal digits, for example 0.999763. Four decimals would be enough for me, to limit the table size in the Word document.
댓글 수: 0
채택된 답변
Voss
2023년 8월 23일
편집: Voss
2023년 8월 23일
A random matrix:
M = rand(10,10); % matrix to write to file
fn = 'example.csv'; % file to write to
disp(M); % show the matrix for reference
One way to write the file:
fid = fopen(fn,'w');
fprintf(fid,[repmat('%.4f,',1,size(M,2)) '\n'],M.');
fclose(fid);
type(fn); % check the file
Another way:
C = compose('%.4f,',M.');
C(end+1,:) = {newline()};
fid = fopen(fn,'w');
fprintf(fid,'%s',C{:});
fclose(fid);
type(fn); % check the file
댓글 수: 0
추가 답변 (2개)
Daniel Bengtson
2023년 8월 23일
편집: Daniel Bengtson
2023년 8월 23일
You can use fprintf to do that.
fid1 = fopen('filename.csv', 'w');
fprintf(fid1,'%.4f',0.999763425634234234);
Walter Roberson
2023년 8월 23일
For the case of a text file, dlmwrite supports a Precision parameter that can be number of digits or can be a format specification.
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!