How can I have proper alignment in my output?
54 views (last 30 days)
Show older comments
Syed Ashtar Hussain Shah
on 13 Sep 2021
Commented: Chunru
on 16 Sep 2021
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;
end
% 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>
end
end
% Used to store how many non-numerical inputs there are in each column
% (under each header).
nonNumerical(c,2) = n;
end
% 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);
end
% 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('\n\t\t\t\t\t\t\t\t')
fprintf('\t%s\t', DUID);
% nr = length of dailyAverages
if nr == 0
fprintf('Error: file contains no numeric data');
else
fprintf('\nNumeric Daily Averages\t\t\t\t')
fprintf('%.2f\t\t',numericDailyAverages);
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('%.2f\t\t',averageOfDailyAverages);
fprintf('\nDaily Averages > Average\t\t\t')
fprintf('%.2f\t\t', dailyAveragesGreaetrThanAverage);
fprintf('\n');
end
0 Comments
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');
fprintf('%-30s', 'Maximum Daily Average:'); fprintf('%10.2f ', rand(1,4)); fprintf('\n');
0 Comments
More Answers (1)
Syed Ashtar Hussain Shah
on 16 Sep 2021
Edited: Syed Ashtar Hussain Shah
on 16 Sep 2021
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.
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!