How do I plot Gantt Chart for scheduling?

Hello everyone,
I have following matrix including task start and finish time for scheduling.
task start finish
1 0 410
2 410 600
3 600 803
4 803 1425
5 600 950
6 0 653
7 653 1970
8 1970 3050
9 3050 3406
10 3406 3778
11 3778 4192
12 4192 4693
13 4693 5353
14 3050 4572
15 5353 5452
16 5452 7648
How can I plot start and finish time for each task?

 Réponse acceptée

Adam Danz
Adam Danz le 10 Jan 2023
Modifié(e) : Adam Danz le 10 Jan 2023
You could create your own Gantt chart by building on this quick demo below.
Demo: Matrix version
data = [% task, start, finish
1 0 410
2 410 600
3 600 803
4 803 1425
5 600 950
6 0 653
7 653 1970
8 1970 3050
9 3050 3406
10 3406 3778
11 3778 4192
12 4192 4693
13 4693 5353
14 3050 4572
15 5353 5452
16 5452 7648];
width = .75; % vertical width of horizontal bars
ypairs = data(:,1) + width./[-2,2];
y = repelem(ypairs,1,2);
x = data(:,[2,3,3,2]);
patch(x',y','b')
grid on
set(gca,'YDir','Reverse')
Demo: Table version
T = array2table([
1 0 410
2 410 600
3 600 803
4 803 1425
5 600 950
6 0 653
7 653 1970
8 1970 3050
9 3050 3406
10 3406 3778
11 3778 4192
12 4192 4693
13 4693 5353
14 3050 4572
15 5353 5452
16 5452 7648], ...
'VariableNames', {'Task','Start','Finish'});
width = .75; % vertical width of horizontal bars
ypairs = T.Task + width./[-2,2];
y = repelem(ypairs,1,2);
x = [T.Start, T.Finish, T.Finish, T.Start];
patch(x',y','b')
grid on
set(gca,'YDir','Reverse')

8 commentaires

Abdullah Türk
Abdullah Türk le 10 Jan 2023
Déplacé(e) : Adam Danz le 10 Jan 2023
Thank you Adam.
But the code did not work. Following error:
Matrix dimensions must agree.
Error in obj_test (line 94)
ypairs = data(:,1) + width./[-2,2];
I used this matrix for data:
data = [% task, start, finish
10 0 410
1 410 600
2 600 803
3 803 1425
11 600 950
12 0 653
15 653 1970
13 1970 3050
4 3050 3406
5 3406 3778
6 3778 4192
7 4192 4693
8 4693 5353
14 3050 4572
9 5353 5452
16 5452 7648];
width = .75; % vertical width of horizontal bars
ypairs = data(:,1) + width./[-2,2];
y = repelem(ypairs,1,2);
x = data(:,[2,3,3,2]);
patch(x',y','b')
grid on
set(gca,'YDir','Reverse')
But it did not work.
I see that you are using MATLAB R2015a (thanks for filling in your release info). My solution uses implicit expansion which became available in MATLAB R2016b.
Replace that line with
ypairs = bsxfun(@plus,data(:,1), width./[-2,2])
Abdullah Türk
Abdullah Türk le 10 Jan 2023
Yes, I'm using MATLAB R2015a.
Now, it works.
Thank you very much Adam :)
Abdullah Türk
Abdullah Türk le 10 Jan 2023
Hi again Adam,
I want to ask a last question about plotting. Can we just show task numbers without 0 and 18 on the x-axis?
Thank you.
set(gca,'YTick',sort(data(:,1)))
Abdullah Türk
Abdullah Türk le 10 Jan 2023
Thanks Adam :)
Amazing code for a project managment class for engineers who are adapting to Matlab.
Adam Danz
Adam Danz le 19 Déc 2023
Thanks for the feedback @Alfonso Rodriguez

Connectez-vous pour commenter.

Plus de réponses (1)

Muhammad Raza
Muhammad Raza le 23 Sep 2023

0 votes

Here in this article, enough details available about how to plot gantt chart using MATLAB, take a look.

Catégories

En savoir plus sur Labels and Annotations dans Centre d'aide et File Exchange

Produits

Version

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by