Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

matlab matlab 09. In an experimental setup, the measurement is recorded in inter

ID: 3731727 • Letter: M

Question

matlab
matlab

09. In an experimental setup, the measurement is recorded in intervals of seconds. Write a script file that prompt the user to enter the time duration in seconds and reun the equivalent combined time in weeks, days, hours, minutes and seconds (use rrinsuch that: Only include quantities with non-zem values in the output(e.g., for input of 86400 sec return 1 . Give Iurger units precedence over smaller ones as much as possible (E-for input of 130 se return 2 nar, 10 sec and NOT min, ?ù =ec or 110 sec) Consider the following cnversion formnlas: mnit suffiused in output weekx 1 week-7 days 1 day 24 hours 1 hour 60 minutes 1 minutes 60 seconds Duy hour hr minute min second | sec Test your code using thfollowing cuss: input numher 100 7259 86400 output 1 mtn 40 sec 2 hr. 59 ec 1 c 9 wk, 6 d, 10 hr, 10 111n

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. :)