retime timetable based on custom definition of year

7 visualizaciones (últimos 30 días)
Sarvesh
Sarvesh el 5 de En. de 2024
Comentada: Sarvesh el 6 de En. de 2024
Hi and thanks in advance.
I have a timetable that i want to retime to yearly data and find the counts of variable 'cat'. THe issue is that unlike the usualy definition of 'year' in Matlab 'retime' (which is Jan 1 to Dec 31), I want to define my year as starting on July 1 in the current year and ending on Jun 30 in the next year and use this definition to retime my timetable.
One of the method that i can think of is offsetting my data by adding months(5) to the original time and then retimeing it 'yearly' and manually labeling my graphs x-axis (this can be bit tedious for me). Im hoping that there is a better way around this.
highly appreciate if someone can help.

Respuesta aceptada

Hassaan
Hassaan el 5 de En. de 2024
% Load the .mat file containing the timetable
loadedData = load('matlab.mat'); % Replace with your actual .mat file path
% List the variables in the loaded data
vars = whos('-file', 'matlab.mat');
% Display the variable names
disp({vars.name});
tt = loadedData.a8data_org1; % Replace with your actual timetable variable name from the .mat file
% Apply the function to calculate the fiscal year for each time entry in the timetable
% Assuming 'time' is the name of the datetime variable in 'tt'
tt.FiscalYear = arrayfun(@(d) fiscalYear(d), tt.time);
% Assuming 'cat' is the variable in 'tt' that contains the category data
% Create a combined variable 'YearCat' as a string for fiscal year and category
tt.YearCat = strcat(string(tt.FiscalYear), "_", string(tt.cat));
% Convert 'YearCat' to unique numeric indices
[categories, ~, idx] = unique(tt.YearCat);
% Count occurrences of each unique category
counts = accumarray(idx, 1);
% Plot the counts
figure;
bar(counts);
set(gca, 'xticklabel', categories); % Set the x-axis labels to the categories
xlabel('Fiscal Year_Category');
ylabel('Count');
title('Counts by Fiscal Year and Category');
% Define the function to determine the fiscal year
function fy = fiscalYear(d)
if month(d) >= 7
fy = year(d);
else
fy = year(d) - 1;
end
end
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
  1 comentario
Sarvesh
Sarvesh el 6 de En. de 2024
Thanks Muhammad, this is what i needed. Just made some changes to it and it worked in the way I wanted. Much appreciated

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 5 de En. de 2024
Based on your description I suspect you're calling retime with the second input being 'yearly'. As far as I'm aware there's no option to specify when the 'year' starts for purposes of the newTimeStep input argument's definition of 'yearly'. However, you could call retime with a vector of times (the syntax using the newTimes input argument rather than the newTimeStep input.) It would require you to determine the starting and ending years you need to include in that newTimes vector, but that shouldn't be that difficult.
n = 10;
y = randi([1990, 2030], n, 1);
m = randi(12, n, 1);
d = randi(28, n, 1); % No 29th, 30th, or 31st of any month
randomDatetimes = datetime(y, m, d)
randomDatetimes = 10×1 datetime array
10-Mar-2006 20-Feb-1991 02-Feb-1999 07-Mar-2029 19-Jun-2016 27-Feb-2011 13-Jun-2012 16-Dec-2007 12-Feb-2022 06-Nov-2025
firstDate = min(randomDatetimes)
firstDate = datetime
20-Feb-1991
yearOfFirstDate = year(firstDate)
yearOfFirstDate = 1991
if firstDate > datetime(yearOfFirstDate, 7, 1)
disp("firstDate occurs on or after July 1st, " + yearOfFirstDate + ...
". Use datetime(" + yearOfFirstDate + ", 7, 1) as your starting date.")
else
disp("firstDate occurs before July 1st, " + yearOfFirstDate + ...
". Use datetime(" + (yearOfFirstDate-1) + ", 7, 1) as your starting date.")
end
firstDate occurs before July 1st, 1991. Use datetime(1990, 7, 1) as your starting date.
sampleVector = firstDate + calyears(0:5)
sampleVector = 1×6 datetime array
20-Feb-1991 20-Feb-1992 20-Feb-1993 20-Feb-1994 20-Feb-1995 20-Feb-1996

Categorías

Más información sobre Calendar en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by