Having a calendar counting weeks

2 次查看(过去 30 天)
Clément Thomaso
Clément Thomaso 2018-4-11
回答: Vatsal 2024-6-11
Hi, i'm actually having some headache using matlab... i have a vector that can be 3*(52*x) where x is the number of year that i want to work with. As you can guess in this vector i have a 3 type of informations (actually that's a workload) for all 52 weeks of the year (1 year is approximately 52 weeks). Then i want to plot it like a bar graph where in the y-axis we would find the workload in [worker per week] and i want the x-axis divided per week but it has to reset to 1 when it's over 52 week. Can we make the tick of x-axis reset as i mentionned ? i hope i've been clear enough. Thank you.

回答(1 个)

Vatsal
Vatsal 2024-6-11
Hi,
The 'bar' function in MATLAB requires unique XData values for the bars. When the week numbers are reset after every 52 weeks, this results in duplicate XData values. The workaround can be to create a new set of labels for the x-axis that includes the year and week number.
Here is how to implement this:
% Assuming 'workload' is your vector containing workload data
workload = rand(1, 3*52*2); % replace this with the actual data
% Create a vector for weeks
weeks = 1:length(workload);
% Calculate year and week number
yearNum = ceil(weeks/52);
weekNum = mod(weeks-1, 52) + 1;
% Create new labels
newLabels = arrayfun(@(y, w) sprintf('y%dw%d', y, w), yearNum, weekNum, 'UniformOutput', false);
% Plot the data
bar(weeks, workload);
xlabel('Week');
ylabel('Workload [worker per week]');
% Adjust x-axis tick labels
set(gca, 'XTick', 1:1:length(weeks)); % set xticks for each week
set(gca, 'XTickLabel', newLabels); % apply new labels
This will give an x-axis that displays labels in the format ‘y1w1’, ‘y1w2’, …, ‘y2w1’, ‘y2w2’, … for each week.
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by