- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
retime timetable based on custom definition of year
11 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Hassaan
on 5 Jan 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
More Answers (1)
Steven Lord
on 5 Jan 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)
firstDate = min(randomDatetimes)
yearOfFirstDate = year(firstDate)
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
sampleVector = firstDate + calyears(0:5)
0 Comments
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!