
% merge contiguous repeated chord labels in a labfile

function [t1,t2,s] = merge_repeats(labfile, outputlabfile)

writefile = 1;

if nargin<2
    % if no output labfile name is given then don't write output to a file
    writefile=0;
end

t1 = [];
t2 = [];
s = {};

[in_t1,in_t2,in_s] = labread(labfile);

current = in_s{1};
temp1 = in_t1(1);

for i=1:length(in_s)-1
    
    next = in_s{i+1};
    
    % if current is the same as next
    if ~strcmp(current,next)
        t1 = [t1; temp1];
        t2 = [t2; in_t2(i)];
        s =  [s; {current}];
        current = next;
        temp1 = in_t1(i+1);
    end
    
    if i==length(in_s)-1
       t1 = [t1; temp1];
       t2 = [t2; in_t2(i+1)];
       s =  [s; {current}];  
    end
    
end
    
if writefile
    
    labwrite(t1,t2,s,outputlabfile);
end
