function make_individual_hierarchical_branches(data, num_branches, parcellation_mask, hemi)

dist        = pdist(data, 'cityblock');
link        = linkage(dist, 'ward');
clust_idx   = cluster(link, 'maxclust', num_branches);
color       = link(end-num_branches+2,3)-eps;

%--------------------------------------------------------------------------
% Needed to save output
part_of_file_name = strsplit(parcellation_mask,'/');
file_name = [part_of_file_name{end-1} '_' num2str(num_branches) '_branches'];
fprintf('Making hierarchical results for: %s \n', strrep(file_name, '_', ' '));
%--------------------------------------------------------------------------
figure('position',[100,100,700,800])
if upper(hemi)=='R'
    fig_handle  = dendrogram(link, 0, 'colorthreshold', color, 'Orientation','right');
elseif upper(hemi)=='L'
    fig_handle  = dendrogram(link, 0, 'colorthreshold', color, 'Orientation','left');
end

set(fig_handle,'LineWidth',3);
H = gcf;
set(H.CurrentAxes,'FontSize',12)
set(H.CurrentAxes,'FontName','AvenirNext')
title(strrep(file_name, '_', ' '),'FontSize',16,'FontWeight','Normal')

%==========================================================================
% Replace parcell numbers in your parcellation with branch numbers
%==========================================================================
% Get ROI func
mask                        = gifti(parcellation_mask);
blank                       = mask;
blank.private.data{1}.data  = zeros(size(blank.private.data{1}.data));
% Find non-zero indices
original_ind                = find(mask.private.data{1}.data);

% Replace indices with branch number
for i_hierarchical = 1: length(clust_idx)
    replace_inds = find(mask.private.data{1}.data(original_ind)==i_hierarchical);
    blank.private.data{1}.data(original_ind(replace_inds)) = clust_idx(i_hierarchical);
    clear replace_inds;
end

% Save hierarchical tree as a matlab figure
savefig(gcf, file_name)

% Backproject to a .func.gii file
create_gifti('func', [file_name '.func.gii'], blank.cdata, hemi);

close all;

end