Gannt Chart from a .csv file

조회 수: 3 (최근 30일)
Daniel Underwood
Daniel Underwood 2020년 5월 7일
답변: Monika Phadnis 2020년 5월 13일
So I am trying to learn to use MATLAB for things I can do in Excel to improve my skills. I am wanting to make a Gannt chart for an up and coming project. I used the following table;
To produce this stacked bar chart following the guide found here https://www.officetimeline.com/make-gantt-chart/excel;
Is it possible to use the table I have created to make an identical chart in MATLAB.
So far my minimal progress has been;
>> Gannt = readtable('Gant example.csv');
>> Start = Gannt(:,1);
>> Task = Gannt(:,3);
>> Duration = Gannt(:,4);
%so I thought the next step would be:
>> barh(Start,Duration,'stacked')
%But then I got this error.
Error using bar (line 139)
Input arguments must be numeric, datetime, duration or categorical.
Error in barh (line 44)
h = bar(varargin{:});
Where have I gone wrong and what are the next steps?
Additionally, I'll have to make a lot of similar charts in the future, so is it possible to save such a code as a function?
Thank you in advance from this MATLAB beginner.

채택된 답변

Monika Phadnis
Monika Phadnis 2020년 5월 13일
You can use the 'Task' column of your table as x-data for 'barh' function. You can make it a categorical array using the 'categorical' function. But to make it a categorical array, the array should have unique values.
I followed this approach for the csv file provided by you
rd = readtable('Gant.csv'); %read data from the csv file
xVals = categorical(unique(rd.Task,'stable')); % 'stable' preserves the order
After this, you can create a m x n matrix using 'Start' and 'Duration_months_' columns to plot the values. Since, the 3rd task above has two entries, we need to create a 9x4 matrix. 9 rows for 9 tasks. Out of 4 columns, first two columns for first entry of task (Start,Duration), next two for the next entry. Since only one task has a second entry, other tasks can have zeros.
A = double(rd.Start);
B = double(rd.Duration_months_);
C = zeros(9,1); %initialize with zeros
D = zeros(9,1);
% assign second entry to C and D arrays, i.e. the 5th entry to 3rd position
C(3,1) = A(5,1);
A = [A(1:4) ; A(6:10)]; % reshape A to have 9 values
D(3,1) = B(5,1);
B = [B(1,4) ; B(6:10)]; % reshape B to have 9 values
% plot
barh(xVals, [A B C D], 'stacked');
Hope this helps.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by