How can I have proper alignment in my output?

15 views (last 30 days)
clc; clear;
% Asking for file name input.
filename = input('Enter the filename to process: ','s'); %filename = '2018-January-Averages.txt';
% Opening file and reading.
fid = fopen(filename,'rt');
% Getting headers.
DUID = string( strsplit( fgetl(fid), ',') );
% Storing data of daily averages.
dailyAverages = textscan(fid, '%s%s%s%s', 'Delimiter', ',','collectoutput',1);
dailyAverages = dailyAverages{:,:};
% Getting size of cell array.
[nr, nc] = size(dailyAverages);
% Used to keep track of how many non-numerical inputs there are.
nonNumerical = zeros(nc,2);
% 2 For loops to analyze each cell in the cell array.
for c = 1:nc
n = 0;
for r = 1:nr
% If daily average is non-numerical, n increases in value by 1
if nnz(isstrprop(dailyAverages{r,c}, 'digit')) == 0
% If daily average is non-numerical, it is changed to 0 so
% cell array can be changed to matrix later on for algebraic
% functions.
dailyAverages{r,c} = ['0']; %#ok<NBRAK>
n = n + 1;
% Each daily average is changed to a double, so cell array can e
% changed to matrix later on for algebraic functions.
dailyAverages{r,c} = str2double(dailyAverages{r,c});
% Used to check if a daily average is negative.
if dailyAverages{r,c} < 0
%If it is negative, it is changed to 0, because output requires
% values above 0.
dailyAverages{r,c} = [0]; %#ok<NBRAK>
% Used to store how many non-numerical inputs there are in each column
% (under each header).
nonNumerical(c,2) = n;
% Converting cell array to matrix for algebraic functions.
dailyAverages = cell2mat(dailyAverages);
% Total number of inputs is 31. The number of non-numerical inputs in each
% column is substracted to get number of numerical inputs in each column
% (under each header).
numericDailyAverages = zeros(1,nc);
for c = 1:nc
numericDailyAverages(1,c) = nr - nonNumerical(c,2);
% Basic algebraic functions to get desired statistics.
maxDailyAverage = max(dailyAverages);
countDailyAverages = sum(dailyAverages~=0);
averageOfDailyAverages = sum(dailyAverages,1) ./ countDailyAverages;
s = dailyAverages >= averageOfDailyAverages;
dailyAveragesGreaetrThanAverage = sum(s~=0);
% \t is used to agjust spacing, and \n to move to next line.
fprintf('Summary Information for ')
fprintf('%s', filename);
fprintf('\t%s\t', DUID);
% nr = length of dailyAverages
if nr == 0
fprintf('Error: file contains no numeric data');
fprintf('\nNumeric Daily Averages\t\t\t\t')
fprintf('\nMaximum Daily Average\t\t\t\t')
fprintf('%.2f\t\t', maxDailyAverage);
fprintf('\nDaily Averages > 0\t\t\t\t\t')
fprintf('%.2f\t\t', countDailyAverages);
fprintf('\nAverage of Daily Averages > 0\t\t')
fprintf('\nDaily Averages > Average\t\t\t')
fprintf('%.2f\t\t', dailyAveragesGreaetrThanAverage);

Accepted Answer

Chunru on 13 Sep 2021
Try something like this to align the text:
fprintf('%-30s', 'Numeric Daily Averages:'); fprintf('%10.2f ', rand(1,4)); fprintf('\n');
Numeric Daily Averages: 0.23 0.02 0.67 0.63
fprintf('%-30s', 'Maximum Daily Average:'); fprintf('%10.2f ', rand(1,4)); fprintf('\n');
Maximum Daily Average: 0.61 0.95 0.25 0.72

More Answers (1)

Syed Ashtar Hussain Shah
Syed Ashtar Hussain Shah on 16 Sep 2021
Edited: Syed Ashtar Hussain Shah on 16 Sep 2021
I actually solved it myself some days ago. You only need to change the number of \t in fprintf to align the output.
  1 Comment
Chunru on 16 Sep 2021
Hi. The use of \t may be very fragile and is difficult to adjust if you change some strings. Use it with caution.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by