필터 지우기
필터 지우기

How to specify number of decimals in table2word or writematrix

조회 수: 30 (최근 30일)
Gerrit
Gerrit 2023년 8월 23일
댓글: Gerrit 2023년 8월 24일
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.

채택된 답변

Voss
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
0.0875 0.0521 0.8735 0.2275 0.9665 0.9244 0.9738 0.6341 0.9535 0.1825 0.2008 0.7956 0.6300 0.8176 0.8290 0.2900 0.0079 0.2002 0.1766 0.2160 0.7700 0.2851 0.8427 0.7191 0.7268 0.6323 0.6483 0.2989 0.0995 0.0482 0.7619 0.5196 0.5693 0.0790 0.9315 0.5310 0.8457 0.5849 0.7667 0.1528 0.1522 0.9690 0.0655 0.5860 0.3579 0.3156 0.7562 0.1719 0.4301 0.7786 0.9014 0.0027 0.1264 0.0835 0.8795 0.8506 0.2957 0.4463 0.7743 0.8732 0.4047 0.1415 0.2763 0.3467 0.2525 0.5155 0.7914 0.7816 0.6304 0.4213 0.0221 0.8341 0.3272 0.3642 0.9500 0.5038 0.8380 0.4023 0.7816 0.0560 0.1109 0.7316 0.5988 0.0174 0.0255 0.8042 0.6785 0.0685 0.8624 0.4913 0.3914 0.6016 0.6013 0.4869 0.4036 0.7793 0.6142 0.4322 0.5552 0.0047
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
0.0875,0.0521,0.8735,0.2275,0.9665,0.9244,0.9738,0.6341,0.9535,0.1825, 0.2008,0.7956,0.6300,0.8176,0.8290,0.2900,0.0079,0.2002,0.1766,0.2160, 0.7700,0.2851,0.8427,0.7191,0.7268,0.6323,0.6483,0.2989,0.0995,0.0482, 0.7619,0.5196,0.5693,0.0790,0.9315,0.5310,0.8457,0.5849,0.7667,0.1528, 0.1522,0.9690,0.0655,0.5860,0.3579,0.3156,0.7562,0.1719,0.4301,0.7786, 0.9014,0.0027,0.1264,0.0835,0.8795,0.8506,0.2957,0.4463,0.7743,0.8732, 0.4047,0.1415,0.2763,0.3467,0.2525,0.5155,0.7914,0.7816,0.6304,0.4213, 0.0221,0.8341,0.3272,0.3642,0.9500,0.5038,0.8380,0.4023,0.7816,0.0560, 0.1109,0.7316,0.5988,0.0174,0.0255,0.8042,0.6785,0.0685,0.8624,0.4913, 0.3914,0.6016,0.6013,0.4869,0.4036,0.7793,0.6142,0.4322,0.5552,0.0047,
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.0875,0.0521,0.8735,0.2275,0.9665,0.9244,0.9738,0.6341,0.9535,0.1825, 0.2008,0.7956,0.6300,0.8176,0.8290,0.2900,0.0079,0.2002,0.1766,0.2160, 0.7700,0.2851,0.8427,0.7191,0.7268,0.6323,0.6483,0.2989,0.0995,0.0482, 0.7619,0.5196,0.5693,0.0790,0.9315,0.5310,0.8457,0.5849,0.7667,0.1528, 0.1522,0.9690,0.0655,0.5860,0.3579,0.3156,0.7562,0.1719,0.4301,0.7786, 0.9014,0.0027,0.1264,0.0835,0.8795,0.8506,0.2957,0.4463,0.7743,0.8732, 0.4047,0.1415,0.2763,0.3467,0.2525,0.5155,0.7914,0.7816,0.6304,0.4213, 0.0221,0.8341,0.3272,0.3642,0.9500,0.5038,0.8380,0.4023,0.7816,0.0560, 0.1109,0.7316,0.5988,0.0174,0.0255,0.8042,0.6785,0.0685,0.8624,0.4913, 0.3914,0.6016,0.6013,0.4869,0.4036,0.7793,0.6142,0.4322,0.5552,0.0047,

추가 답변 (2개)

Daniel Bengtson
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);
  댓글 수: 1
Gerrit
Gerrit 2023년 8월 24일
Thank you: However, if I use this fprintf function witht eh simple formatting '%.4f' only, all elements in the matrix are strung together into one string instead of forming a matrix in the csv file:
matrix3 = rand(2,3)
matrix3 = 2×3
0.3781 0.9722 0.5639 0.6576 0.4597 0.3789
fid1 = fopen('table3.csv', 'w');
fprintf(fid1, '%.4f', matrix3);
fclose(fid1);
The only value/string in the resulting csv file is (this was from execution in my own Matlab, so the numerical values are different):
0.96490.15760.97060.95720.48540.8003
i.e. six values in '%.4f' format, but all in one single string. If I use instead, for the fprintf line:
fprintf(fid1, '%.4f %.4f %.4f\n', matrix3);
I get two rows/fields above each other in the csv file:
0.9649 0.1576 0.9706
0.9572 0.4854 0.8003
So still not in a comma-separated format that I can import/transfer directly to a Word table. Ah, I now see Voss's answer below including the 'repmat' function to do the matrix/table formatting. Yes, that works. I'll still post my comment here in case it helps anyone else.

댓글을 달려면 로그인하십시오.


Walter Roberson
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.
  댓글 수: 1
Gerrit
Gerrit 2023년 8월 24일
Thank you. Your solution would address my issue as well. However, the Matlab help specifically dissuades the use of this function:
dlmwrite is not recommended. Use writematrix instead. For more information, see Compatibility Considerations.
It is a pity that a function, dlmwrite, with more (formatting) options is replaced by one with fewer such options, i.e. writematrix. I notice that "There are no plans to remove dlmwrite.", but the 'not recommended' is still a bit off-putting.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by