how can i make a parent - children hierarchy

40 次查看(过去 30 天)
Muhannad AL-hazmi
Muhannad AL-hazmi 2024-8-3,19:48
编辑: Voss 2024-8-4,21:34
Hello
i have two column one is number of hiarachy the second is the name of that hiarachy like this :
can you help make a code that will generate what is manually wrote in the third column , note that the file is large and contain long WBS path like this (2.2.3.5.4.5.7)
Regards
  3 个评论
Muhannad AL-hazmi
Muhannad AL-hazmi 2024-8-4,19:50
i want to take the WBS name and return it the same in WBS Full Path name
for example in the third row becuase it have 3 digit it should have three name path 2 is Scope of work and 2.1 key Milstone and 2.1.1 is Contractual Milestones so :
Scope of work > key Milstone > Contractual Milestones
it's easy for me to read it in the original file
dpb
dpb 2024-8-4,20:55
You forgot to attach a sample file...

请先登录,再进行评论。

回答(1 个)

Voss
Voss 2024-8-4,21:33
编辑:Voss 2024-8-4,21:34
Here's some logic that should do it. Adjust as necessary for your actual file.
% an example table that might be similar to what you get when you
% readtable() your file:
WBS_Path = {'2';'2.1';'2.1.1'};
WBS_Name = {'SCOPE OF WORK';'KEY MILESTONES';'CONTRACTUAL MILESTONES'};
T = table(WBS_Path,WBS_Name)
T = 3x2 table
WBS_Path WBS_Name _________ __________________________ {'2' } {'SCOPE OF WORK' } {'2.1' } {'KEY MILESTONES' } {'2.1.1'} {'CONTRACTUAL MILESTONES'}
% construct the third column from the first two:
N = cellfun(@(p)nnz(p=='.')+1,T.WBS_Path);
C = arrayfun(@(n)repmat({''},1,n),N,'UniformOutput',false);
for ii = 1:size(T,1)
idx = find(startsWith(T.WBS_Path,T.WBS_Path{ii}));
for jj = 1:numel(idx)
C{idx(jj)}(N(ii)) = T.WBS_Name(ii);
end
end
T.WBS_Full_Path_Name = cellfun(@(c)strjoin(c,' > '),C,'UniformOutput',false)
T = 3x3 table
WBS_Path WBS_Name WBS_Full_Path_Name _________ __________________________ ___________________________________________________________ {'2' } {'SCOPE OF WORK' } {'SCOPE OF WORK' } {'2.1' } {'KEY MILESTONES' } {'SCOPE OF WORK > KEY MILESTONES' } {'2.1.1'} {'CONTRACTUAL MILESTONES'} {'SCOPE OF WORK > KEY MILESTONES > CONTRACTUAL MILESTONES'}

Community Treasure Hunt

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

Start Hunting!

Translated by