Having a calendar counting weeks
2 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
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!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Calendar についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!