Keep trailing zeros in an output file.

조회 수: 11 (최근 30일)
Peter Bohn
Peter Bohn 2024년 11월 26일
댓글: Peter Bohn 2024년 11월 26일
This sounds like a stupid question, but i can't seem to find the answer anywhere.
I am outputting a text file from an array and in order to keep my columns aligned, i would like to keep trailing zeros of the values in the array.
I've attached what my output file currently looks like.
SpecData(:,1) = round(SpecData(:,1), 9, 'significant');
SpecData(:,2) = round(SpecData(:,2), 4, 'significant');
SpecData(:,3) = round(SpecData(:,3), 7, 'significant');
SpecData(:,4) = round(SpecData(:,4), 7, 'significant');
name = [numMonth2str(str2num(month))+year];
name_out = [F_out+name+type];
writematrix(SpecData,name_out,'Delimiter','tab');
for reference, column 1 of SpecData is a datenumber (these are causing an issue because some have 3 decimal places, and some have none, and column 2 is values ranging from 0.1000 to 6.0 and column 3 is between -500.00 to 500.00 approximately...

채택된 답변

Walter Roberson
Walter Roberson 2024년 11월 26일
You cannot do that using writematrix()
You can use dlmwrite with a 'precision' option. However, there is no option to adjust the precision per-column.
If you need different precision for different columns, then use compose with a format specified as a double-quoted string, and use writelines to write the resulting string array to a file.
  댓글 수: 1
Peter Bohn
Peter Bohn 2024년 11월 26일
Thank you, that worked very well. Below is the results of the solution you provided.
% SpecData(:,1) = round(SpecData(:,1), 9, 'significant');
% SpecData(:,2) = round(SpecData(:,2), 4, 'significant');
% SpecData(:,3) = round(SpecData(:,3), 7, 'significant');
% SpecData(:,4) = round(SpecData(:,4), 7, 'significant');
name = [year+"-"+month];
name_out = [F_out+name+type];
%writematrix(SpecData,name_out,'Delimiter','tab');
Dat_str = compose("%.3f\t%.3f\t%8.3f\t%6.3f\t%.2f\t%.2f", SpecData);
writelines(Dat_str,name_out);

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

추가 답변 (1개)

praguna manvi
praguna manvi 2024년 11월 26일
편집: praguna manvi 2024년 11월 26일
As I see, you are looking to write a matrix to a tab-delimited text file with different precision values for each column. Since "writematrix" writes out numeric data as unquoted text, you can use "writecell" with "compose" to specify the formatted precision for each column as a string, as shown below:
% Input array with trailing zeros
SpecData = [
738000.123, 0.1, 123.456 123.456789, 0, 0;
738001.0, 1.125 13.52011 0.1234567, 0, 0;
738002.456, 0.1000, 500.00 456.7890123, 0, 0;
738476 1.207 21.9688 0.2504902 0 0
];
% Define the format for each column
formats = {'%.3f', '%.4f', '%.2f', '%.7f', '%.1f', '%.1f'};
% Apply formatting to each column using arrayfun and cellfun
formattedData = arrayfun(@(col) compose(formats{col}, SpecData(:, col)), 1:size(SpecData, 2), 'UniformOutput', false);
% Concatenate the formatted columns into a cell array
formattedData = horzcat(formattedData{:});
% Write the formatted data to a file using writecell
name_out = 'formatted_output.txt';
writecell(formattedData, name_out, 'Delimiter', '\t');
% Display the formatted data
disp('Formatted Data:');
Formatted Data:
disp(formattedData);
{'738000.123'} {'0.1000'} {'123.46'} {'123.4567890'} {'0.0'} {'0.0'} {'738001.000'} {'1.1250'} {'13.52' } {'0.1234567' } {'0.0'} {'0.0'} {'738002.456'} {'0.1000'} {'500.00'} {'456.7890123'} {'0.0'} {'0.0'} {'738476.000'} {'1.2070'} {'21.97' } {'0.2504902' } {'0.0'} {'0.0'}
  댓글 수: 1
Peter Bohn
Peter Bohn 2024년 11월 26일
Thank you, This looks like a good solution as well.

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

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by