Keep trailing zeros in an output file.

41 views (last 30 days)
Peter Bohn
Peter Bohn on 26 Nov 2024 at 2:05
Commented: Peter Bohn on 26 Nov 2024 at 4:37
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...

Accepted Answer

Walter Roberson
Walter Roberson on 26 Nov 2024 at 2:55
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 Comment
Peter Bohn
Peter Bohn on 26 Nov 2024 at 4:36
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);

Sign in to comment.

More Answers (1)

praguna manvi
praguna manvi on 26 Nov 2024 at 4:18
Edited: praguna manvi on 26 Nov 2024 at 4:20
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 Comment
Peter Bohn
Peter Bohn on 26 Nov 2024 at 4:37
Thank you, This looks like a good solution as well.

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!