%% The code is written by Behnaz Pirzamanbein, bepi@dtu.dk last version 2020.08.04
%% fix the meaure distance plot same xlim and ylim and also caxes
samples = {'out_out_CD','out_out_MD','in_in_CD','in_in_MD'};
degree = {'0','45','90','180'};

units = {'pixel','mm','micrometer'};
% choose a unit for measure
tf = 0;
while tf == 0
    [indx,tf] = listdlg('ListString',units,'PromptString','Choose an unit:','SelectionMode','single','InitialValue',1);
end
unit = units{indx};

flag_save = 1;
load_folder = 'Results\measure\';
save_folder = 'Results\measure\';
if ~exist(save_folder,'dir')
    mkdir(save_folder)
end 

% if you want to run the measure separately you can use the for loop
%for i = 1:4
    sample = samples{i};

    dist0 = load([load_folder,'Dist_',sample,'_0']);
    dist45 = load([load_folder,'Dist_',sample,'_45']);
    dist90 = load([load_folder,'Dist_',sample,'_90']);
    dist180 = load([load_folder,'Dist_',sample,'_180']);
    
    if strcmp(unit,'mm')
            dist0.dist = dist0.dist*4.2/1e3;
            dist45.dist = dist45.dist*4.2/1e3;
            dist90.dist = dist90.dist*4.2/1e3;
            dist180.dist = dist180.dist*4.2/1e3;
    elseif strcmp(unit,'micrometer')
            dist0.dist = dist0.dist*4.2;
            dist45.dist = dist45.dist*4.2;
            dist90.dist = dist90.dist*4.2;
            dist180.dist = dist180.dist*4.2;
    end
    
    cax0 = [min(dist0.dist,[],'all'),max(dist0.dist,[],'all')];
    lim0 = size(dist0.dist);
    
    cax45 = [min(dist45.dist,[],'all'),max(dist45.dist,[],'all')];
    lim45 = size(dist45.dist);
    
    cax90 = [min(dist90.dist,[],'all'),max(dist90.dist,[],'all')];
    lim90 = size(dist90.dist);
    
    cax180 = [min(dist180.dist,[],'all'),max(dist180.dist,[],'all')];
    lim180 = size(dist180.dist);
    
    cax = [min([cax0(1),cax45(1),cax90(1),cax180(1)]),max([cax0(2),cax45(2),cax90(2),cax180(2)])];
    lim = [max([lim0(1),lim45(1),lim90(1),lim180(1)]),max([lim0(2),lim45(2),lim90(2),lim180(2)])];

    figure
    subplot(221)
    imagesc(dist0.dist)
    ylabel('along the curve')
    xlim([1,lim(1)])
    ylim([1,lim(2)])
    caxis(cax)
    axis off
    title('0 degree')
    
    subplot(222)
    imagesc(dist45.dist)
    xlim([1,lim(1)])
    ylim([1,lim(2)])
    caxis(cax)
    axis off
    title('45 degree')
    
    subplot(223)
    imagesc(dist90.dist)
    xlabel('through slice')
    ylabel('along the curve')
    xlim([1,lim(1)])
    ylim([1,lim(2)])
    caxis(cax)
    axis off
    title('90 degree')
    
    subplot(224)
    imagesc(dist180.dist)
    xlabel('through slice')
    xlim([1,lim(2)])
    ylim([1,lim(1)])
    caxis(cax)
    c = colorbar('southoutside');
    c.Label.String = unit;
    axis off
    title('180 degree')
    
    suptitle_modified(sample,'none')
   
    if flag_save
        saveas(gcf,sprintf('%sDist_%s_%s.png',save_folder,sample,unit))
    end
%end
