# Horizontal bar chart / Gantt Chart

8 visualizzazioni (ultimi 30 giorni)
Alexander Tollinger il 30 Mar 2020
Hey guys I have a simple question,
I have a 5x 3 array with the 1st column representing the project number, 2nd column representing the start time and 3rd col representing the end time.
• Now I just need to plot this in a Gantt Chart.
Heres the script:
clc;
clear all;
close all;
rng('default');
T = [1 randi([10 20])
2 randi([10 20])
3 randi([10 20])
4 randi([10 20])
5 randi([10 20])]
D = projectscheduling(T);
And here is the function:
Brief summary of this function.
Detailed explanation of this function.
function [time] = projectscheduling(T)
time = zeros(5, 3);
for i = 2:5
time(1 ,1) = 1;
time(1, 2) = 0;
time(1 , 3) = T(1, 2);
time(i , 1) = T(i , 1);
time(i, 2) = time(i-1, 3);
time(i, 3) = T(i , 2) + time(i, 2);
end
disp(time);
end
And here's a pic of what the finished plot should look like:
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Jyotsna Talluri il 2 Apr 2020
The vertical axis coordinates should represent project number and bar lengths of each group should be represented by vector of values . Generate these vector of values from your Matrix D as below
Positions=zeros(1,5); % vertical axis coordinates
plotLengths = zeros(5,2);
for i =1:5
Positions(1,i) = D(i,1);
plotLengths(i,1) = D(i,2);
plotLengths(i,2) = D(i,3)-D(i,2);
end
Draw the bars in the stacked style with the total length of bar of each group equals the sum of elements in the group
H = barh(Positions,plotLengths,'stacked');
Set the visibility of first plot handle of each group to off
set(H(1),'Visible','off');
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

Alexis Wang il 30 Ago 2022
This can take in a datetime vector of start dates and another of end dates. It uses patch instead of barh though, and draws each bar for every task. Feel free to let me know if you have any questions. Hope that helps!
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Mohamed El Khalil Danine il 7 Dic 2022
hi i tryed to use this code but it gives me lot errors.

Accedi per commentare.

### Categorie

Scopri di più su Bar Plots in Help Center e File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by