type='cmc';

dirs = dir(['opt_' type '_*']);

compare_file = '';
compare_column = '';
%compare_file = 'subject01_walk1_ik.opt.mot';
%compare_column = 'knee_angle_l';
%compare_file = 'subject01_walk1_RRA1_Actuation_force.sto';
%compare_column = 'FY';
%compare_file = 'subject01_walk1_RRA2_states.sto';
%compare_column = 'pelvis_ty_u';
compare_file = 'subject01_walk1_Actuation_force.sto';
%compare_file = 'delaware3_ss_walking1_RRA2_Actuation_force.sto';
%compare_file = 'delaware3_ss_walking1_Actuation_force.sto';
%compare_file = 'delaware3_ss_walking1_Kinematics_q.sto';
%compare_column = 'knee_angle_l';
%compare_column = 'med_gas_l';
compare_column = 'tib_ant_r';

labels={};
timings=[];
errors=[];
T=[];
for i=length(dirs):-1:1;

    if compare_file;
        file=[dirs(i).name '/' compare_file];
        if ~exist(file,'file')
            continue;
        end
        %disp(sprintf('Processing %s',dirs(i).name));
        qq=read_motionFile(file);
        A=qq.data(:,find_columns_by_label(qq.labels,compare_column));
    else
        file=[dirs(i).name '/total_errors'];
        if ~exist(file,'file')
            continue;
        end
        %disp(sprintf('Processing %s',dirs(i).name));
        A=load(file);
    end
    if (size(errors) == [0 0]) | (size(A,1) == size(errors,1))
        file=[dirs(i).name '/timing'];
        if exist(file,'file')
            B=load(file);
            timings(:,end+1)=B;
            label = sprintf('%s (%g s)', dirs(i).name, B);
        else
            timings(:,end+1)=999999;
            label = sprintf('%s', dirs(i).name);
        end
        errors(:,end+1)=A;
        labels = {labels{:} label};

        disp(sprintf('(%d) %s',size(errors,2),dirs(i).name));
        if compare_file;
            T=qq.data(:,1);
            q(size(errors,2)) = qq;
        else
            file=[dirs(i).name '/times'];
            if exist(file,'file')
                T=load(file);
            end
        end
    else
        %disp(sprintf('Warning: %s has weird size',dirs(i).name));
    end
end

[timings_sorted,I]=sort(timings);
errors_sorted=errors(:,I);
labels_sorted=labels(I);

%if strcmp(type,'ik');
%    T=25:121;
%end
%set(axes,'ColorOrder',[1 0 1; 0 1 0]);
%figure;
clf;
hold on;
types={'cfsqp','lbfgsb','ipopt','paramopt'};
base_colors=[0 0 0; 1 0 0; 0 1 0; 0 0 1];
incr=0.05;
for i=1:length(types)
    counter.(types{i})=0;
end
for i=1:size(errors_sorted,2)
    matched_type = 0;
    for j=1:length(types)
        if strfind(labels_sorted{i},types{j})
            matched_type=j;
            break;
        end
    end
    if matched_type
        alpha = min(1,counter.(types{matched_type}) * incr);
        color = (1-alpha) * base_colors(matched_type,:) + alpha * [1 1 1];
        counter.(types{matched_type}) = counter.(types{matched_type}) + 1;
    else
        color = [0 0 0];
    end
    plot(T,errors_sorted(:,i),'Color',color);
end
legend(labels_sorted, 'Interpreter', 'none');
if compare_file;
    title(compare_file);
else
    title('errors');
end
hold off;
