How can I have proper alignment in my output?
52 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Chunru
2021-9-13
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 个评论
更多回答(1 个)
Syed Ashtar Hussain Shah
2021-9-16
编辑:Syed Ashtar Hussain Shah
2021-9-16
1 个评论
Chunru
2021-9-16
Hi. The use of \t may be very fragile and is difficult to adjust if you change some strings. Use it with caution.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!