Matlab Code for Calculating Mouse Age in Years, Months, and Weeks

Matlab Code for Calculating Mouse Age in Years, Months, and Weeks

Input the mouse’s birth date and the date of data capture to calculate the mouse’s age at that time.

% =========================================================================
%            Calculate Mouse Age (Years, Months, Weeks) 
% =========================================================================

% --- 1. Define Dates ---
% Define the mouse's birth date
birthDate = datetime('2025-02-13');
% Define the capture date
shotDate = datetime('2025-05-30');

% --- 2. Calculate Age and Month Age ---
% For years and months, calendarDuration is what we want.
durationCal = between(birthDate, shotDate);
ageInYears = calyears(durationCal);
ageInMonths = calmonths(durationCal);

% --- 3. Calculate Week Age ---
durationFixed = shotDate - birthDate;
totalDays = days(durationFixed);
ageInWeeks = totalDays / 7;

% --- 4. Format and Display Results ---
% The output section remains the same.
outputString = sprintf([...
    'Mouse Age Calculation Result:\n' ...
    '-------------------\n' ...
    'Age: %.4f years\n' ...
    'Month Age: %.4f months\n' ...
    'Week Age:   %.4f weeks'], ...
    ageInYears, ageInMonths, ageInWeeks);

% Display the final result in the command window
disp(outputString);

Output

Mouse Age Calculation Result:
-------------------
Age: 0.0000 years
Month Age: 3.0000 months
Week Age:   15.1429 weeks

Leave a Comment