matlab matlab 09. In an experimental setup, the measurement is recorded in inter
ID: 3731727 • Letter: M
Question
matlab
matlab
Explanation / Answer
Solution:
code:
I have created a function in which you have to provide the value as an argument:
function out = seconds2human(secs, varargin)
error(nargchk(1,2,nargin));%#ok
% define some intuitive variables
Seconds = round(1 );
Minutes = round(60 * Seconds );
Hours = round(60 * Minutes );
Days = round(24 * Hours );
Weeks = round(7 * Days );
Months = round(30.471 * Days );
Years = round(365.26 * Days );
Centuries = round(100 * Years );
Millennia = round(10 * Centuries);
% put these into an array, and define associated strings
units = [Millennia, Centuries, Years, Months, Weeks, ...
Days, Hours, Minutes, Seconds];
singles = {'millennium'; 'century'; 'year'; 'month'; ...
'week'; 'day'; 'hour'; 'minute'; 'second'};
plurals = {'millennia' ; 'centuries'; 'years'; 'months'; ...
'weeks'; 'days'; 'hours'; 'minutes'; 'seconds'};
% cut off all decimals from the given number of seconds
assert(isnumeric(secs), 'seconds2human:seconds_mustbe_numeric', ...
'The argument ''secs'' must be a scalar or matrix.');
secs = round(secs);
% parse second argument
short = true;
if (nargin > 1)
% extract argument
short = varargin{1};
% check its type
assert(ischar(short), 'seconds2human:argument_type_incorrect', ...
'The second argument must be either ''short'' or ''full''.');
% check its contents
switch lower(short)
case 'full' , short = false;
case 'short', short = true;
otherwise
error('seconds2human:short_format_incorrect',...
'The second argument must be either ''short'' or ''full''.');
end
end
% pre-allocate appropriate output-type
numstrings = numel(secs);
if (numstrings > 1), out = cell(size(secs)); end
% build (all) output string(s)
for j = 1:numstrings
% initialize nested loop
secsj = secs(j);
counter = 0;
if short, string = 'About ';
else string = '';
end
% possibly quick exit
if (secsj < 1), string = 'Less than one second.'; end
% build string for j-th amount of seconds
for i = 1:length(units)
% amount of this unit
amount = fix(secsj/units(i));
% include this unit in the output string
if amount > 0
% increase counter
counter = counter + 1;
% append (single or plural) unit of time to string
if (amount > 1)
string = [string, num2str(amount), ' ', plurals{i}];%#ok
else
string = [string, num2str(amount), ' ', singles{i}];%#ok
end
% Finish the string after two units if short format is requested
if (counter > 1 && short), string = [string, '.']; break, end%#ok
% determine whether the ending should be a period (.) or a comma (,)
if (rem(secsj, units(i)) > 0)
if short, ending = ' and ';
else ending = ', ';
end
else ending = '.';
end
string = [string, ending];%#ok
end
% subtract this step from given amount of seconds
secsj = secsj - amount*units(i);
end
% insert in output cell, or set output string
if (numstrings > 1)
out{j} = string;
else
out = string;
end
end % for
end % seconds2human
Output:
>> clear
>> seconds2human(86400)
ans =
About 1 day.
>> seconds2human(100)
ans =
About 1 minute and 40 seconds.
>> seconds2human(7259)
ans =
About 2 hours and 59 seconds.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.