Code covered by the BSD License  

Highlights from
Rosin-Rammler Diagram plotting tool

image thumbnail

Rosin-Rammler Diagram plotting tool

by

 

25 Jun 2010 (Updated )

This tool plots the Rosin-Rammler Diagram (RRSB) and calculates various related parameters.

RRD(varargin)
function varargout = RRD(varargin)
%% MATLAB(R) tool for plotting Rosin-Rammler size distribution diagram
%
%                 Ivan Brezani (a) and Fridrich Zelenak
%
%                   Technical University of Kosice,
%                            BERG Faculty,
%     Institute of Montanneous Sciences and Environmental Protection
%
%                      Last updated: 2010-07-27
%
% 
% (a) ivan.brezani@tuke.sk
% 

% Copyright (c) 2010, Ivan Brezani
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without 
% modification, are permitted provided that the following conditions are 
% met:
%
%    * Redistributions of source code must retain the above copyright 
%      notice, this list of conditions and the following disclaimer.
%    * Redistributions in binary form must reproduce the above copyright 
%      notice, this list of conditions and the following disclaimer in 
%      the documentation and/or other materials provided with the distribution
%    * Neither the name of the University Paris Sud nor the names 
%      of its contributors may be used to endorse or promote products derived 
%      from this software without specific prior written permission.
%      
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
% POSSIBILITY OF SUCH DAMAGE.

% RRD M-file for RRD.fig
%      RRD, by itself, creates a new RRD or raises the existing
%      singleton*.
%
%      H = RRD returns the handle to a new RRD or the handle to
%      the existing singleton*.
%
%      RRD('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in RRD.M with the given input arguments.
%
%      RRD('Property','Value',...) creates a new RRD or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before RRD_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to RRD_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help RRD

% Last Modified by GUIDE v2.5 23-Aug-2010 09:58:59

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @RRD_OpeningFcn, ...
                   'gui_OutputFcn',  @RRD_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before RRD is made visible.
function RRD_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to RRD (see VARARGIN)

% Choose default command line output for RRD
handles.output = hObject;
if handles.open == 1
    handles.ax_hand = handles.axes1;
    [handles] = plot_RR(handles);
    handles.open = 0;
end
guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function varargout = RRD_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes during object creation, after setting all properties.
function uitable1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uitable1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.open = 1;
handles.d1 = NaN(80, 1);
handles.d2 = NaN(80, 1);
handles.d3 = NaN(80, 1);
handles.data_actual = 1;
handles.retained_1 = NaN(80, 1);
handles.passing_1 = NaN(80, 1);
handles.retained_2 = NaN(80, 1);
handles.passing_2 = NaN(80, 1);
handles.retained_3 = NaN(80, 1);
handles.passing_3 = NaN(80, 1);
handles.units_1 = ones(80, 1);
handles.units_2 = ones(80, 1);
handles.units_3 = ones(80, 1);
handles.x_axis_units = 2;
handles.y_axis = 1;
handles.approx = 0;
handles.points = 0;
handles.grid = 1;
handles.multi = 0;
handles.legend = 0;
handles.ytck = [0.001 0.01 0.1 1 5 10 20 30 40 50 60 70 80 85 90 92 94 96 97 98 98.5 99 99.3 99.5 99.7 99.8 99.9 99.95 99.97 99.98 99.99 99.995 99.997 99.998 99.999];
handles.ytck_label = {'0.001', '0.01','0.1', '1','5','10','20','30','40','50','60','70','80','85','90','92','94','96','97', '98','98.5', '99', '99.3', '99.5', '99.7', '99.8','99.9', '99.95', '99.97', '99.98', '99.99', '99.995', '99.997', '99.998', '99.999'};
handles.ytck_label2 = {'99.999', '99.99','99.9', '99','95','90','80','70','60','50','40','30','20','15','10','8','6','4','3', '2','1.5', '1', '0.7', '0.5', '0.3', '0.2','0.1', '0.05', '0.03', '0.02', '0.01', '0.005', '0.003', '0.002', '0.001'};
handles.xmin = 0.1;
handles.xmax = 10000;
handles.ymin = 0.1;
handles.ymax = 99.9;
handles.show1 = 1;
handles.show2 = 1;
handles.show3 = 1;
handles.zoom = 0;
handles.auto_lim = 1;
handles.legend_text_1 = 'Size Distribution';
handles.legend_text_2 = 'Approximation';
handles.legend_text_3 = 'Size Distribution 2';
handles.legend_text_4 = 'Approximation 2';
handles.legend_text_5 = 'Size Distribution 3';
handles.legend_text_6 = 'Approximation 3';
handles.xlabel_text = 'Mesh Size d [mm]';
handles.ylabel_text = 'Retained [%]';
handles.title_text = 'Rosin-Rammler Diagram';
handles.line_3679 = 1;
handles.fit_eq = 0;

tableData = get(hObject,'data');
for I=1:80
   tableData(I, 2) = {'mm'}; 
end
set(hObject, 'data', tableData);
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function uipanel_graph_options_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uipanel_graph_options (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
guidata(hObject, handles);

% --- Executes on button press in pushbutton_plot.
function pushbutton_plot_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.zoom = 0;
handles.ax_hand = handles.axes1;
[handles] = refresh_table(handles);
[handles] = plot_RR(handles);
guidata(hObject, handles);

% --- Executes on button press in checkbox_approx.
function checkbox_approx_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_approx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_approx
handles.approx = get(hObject,'Value');
guidata(hObject, handles);

% --- Executes on button press iget(hObject,'Value')n checkbox_points.
function checkbox_points_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_points (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_points
handles.points = get(hObject,'Value');
guidata(hObject, handles);

% --- Executes on button press in checkbox_grid.
function checkbox_grid_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_grid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_grid
handles.grid = get(hObject,'Value');
guidata(hObject, handles);

% --- Executes on button press in checkbox_legend.
function checkbox_legend_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_legend (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_legend
handles.legend = get(hObject,'Value');
guidata(hObject, handles);

% --- Executes on button press in pushbutton_edit.
function pushbutton_edit_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
edit_RRD();


% --- Executes on button press in pushbutton_zoom.
function pushbutton_zoom_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_zoom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[x1, y1] = ginput(1);
[x2, y2] = ginput(1);
line([x1 x2], [y1 y2], 'Color', 'y', 'LineWidth', 2);
line([x1 x2], [y1 y2], 'Color', 'k', 'LineWidth', 0.75);
[x3, y3] = ginput(1);
line([x2 x3], [y2 y3], 'Color', 'y', 'LineWidth', 2);
line([x2 x3], [y2 y3], 'Color', 'k', 'LineWidth', 0.75);
[x4, y4] = ginput (1);
line([x3 x4], [y3 y4], 'Color', 'y', 'LineWidth', 2);
line([x3 x4], [y3 y4], 'Color', 'k', 'LineWidth', 0.75);
line([x4 x1], [y4 y1], 'Color', 'y', 'LineWidth', 2);
line([x4 x1], [y4 y1], 'Color', 'k', 'LineWidth', 0.75);

pause(0.5);
x = [x1 x2 x3 x4];
y = [y1 y2 y3 y4];
x = sort(x);
y = sort(y);

handles.zoom = 1;
handles.xmaxz = mean([x(3) x(4)]);
handles.xminz = mean([x(1) x(2)]);
handles.ymaxz = mean([y(3) y(4)]);
handles.yminz = mean([y(1) y(2)]);

x = get(handles.axes1, 'XLim');
y = get(handles.axes1, 'YLim');

if handles.xmaxz > x(2)
    handles.xmaxz = x(2);
else
end

if handles.xminz < x(1)
    handles.xminz = x(1);
else
end

if handles.ymaxz > y(2)
    handles.ymaxz = y(2);
else
end

if handles.yminz < y(1)
    handles.yminz = y(1);
else
end

line([handles.xmaxz handles.xmaxz], [handles.ymaxz handles.yminz], 'Color', 'k', 'LineWidth', 2);
line([handles.xminz handles.xminz], [handles.ymaxz handles.yminz], 'Color', 'k', 'LineWidth', 2);
line([handles.xmaxz handles.xminz], [handles.ymaxz handles.ymaxz], 'Color', 'k', 'LineWidth', 2);
line([handles.xmaxz handles.xminz], [handles.yminz handles.yminz], 'Color', 'k', 'LineWidth', 2);
pause(0.5);
handles.ax_hand = handles.axes1;
[handles] = refresh_table(handles);
[handles] = plot_RR(handles);
guidata(hObject, handles);


% --- Executes on button press in checkbox_multi.
function checkbox_multi_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_multi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_multi

[handles] = refresh_table(handles);
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function text_name_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text_name (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function pushbutton_plot_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
guidata(hObject, handles);

% --- Executes when figure_RRD is resized.
function figure_RRD_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to figure_RRD (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(0,'Units','pixels');
scrn = get(0,'ScreenSize');
size = getpos(hObject);
pix = 1/(scrn(3)*size(3));
if scrn(3)*0.514*size(3) > 430
    setpos(handles.uitable1,'0.01nz 0.03nz 430px 0.868nz');
    pos = getpos(handles.uitable1);
    FMT = [pos(1)+pos(3)+(60*pix) (0.03+0.868)-0.52 0.99-(pos(1)+pos(3)+(70*pix)) 0.52];
    setpos(handles.axes1, FMT);
    FMT = [pos(1)+pos(3)+(60*pix) 0.03 0.99-(pos(1)+pos(3)+(70*pix)) 0.27];
    setpos(handles.uipanel_graph_options, FMT);
    setpos(handles.checkbox_multi, '0.01nz 0.9nz 430px 0.04nz');
    setpos(handles.text_name, '0.01nz 0.94nz 430px 0.05nz');
else
    setpos(handles.uitable1,'0.01nz 0.03nz 0.535nz 0.868nz');
    pos = getpos(handles.uitable1);
    FMT = [0.535+(60*pix) (0.03+0.868)-0.52 0.99-(pos(1)+pos(3)+(65*pix)) 0.52];
    setpos(handles.axes1, FMT);
    FMT = [0.535+(60*pix) 0.03 0.99-(pos(1)+pos(3)+(65*pix)) 0.27];
    setpos(handles.uipanel_graph_options, FMT);
    setpos(handles.checkbox_multi, '0.01nz 0.9nz 0.535nz 0.04nz');
    setpos(handles.text_name, '0.01nz 0.94nz 0.535nz 0.05nz');
end
guidata(hObject, handles);


function [handles] = plot_RR(handles)
% plots RR diagram depending on handles

J=1;
d1=[];
data1=[];
for I=1:80
    if isnan(handles.d1(I))==1 || isnan(handles.passing_1(I))==1
    
    else
        d1(J) = handles.d1(I)*handles.units_1(I);
        data1(J) = handles.retained_1(I);
        J=J+1;
    end
end

J=1;
d2=[];
data2=[];
for I=1:80
    if isnan(handles.d2(I))==1 || isnan(handles.passing_2(I))==1
    
    else
        d2(J) = handles.d2(I)*handles.units_2(I);
        data2(J) = handles.retained_2(I);
        J=J+1;
    end
end

J=1;
d3=[];
data3=[];
for I=1:80
    if isnan(handles.d3(I))==1 || isnan(handles.passing_3(I))==1
    
    else
        d3(J) = handles.d3(I)*handles.units_3(I);
        data3(J) = handles.retained_3(I);
        J=J+1;
    end
end

d1_mm = d1;
d2_mm = d2;
d3_mm = d3;

if handles.x_axis_units == 1 %micron
    d1 = d1.*1000;
    d2 = d2.*1000;
    d3 = d3.*1000;
elseif handles.x_axis_units == 2 %mm
elseif handles.x_axis_units == 3 %cm
    d1 = d1./10;
    d2 = d2./10;
    d3 = d3./10;
elseif handles.x_axis_units == 4 %dm
    d1 = d1./100;
    d2 = d2./100;
    d3 = d3./100;
elseif handles.x_axis_units == 5 %m
    d1 = d1./1000;
    d2 = d2./1000;
    d3 = d3./1000; 
end

mind = min([min(d1), min(d2), min(d3)]);
maxd = max([max(d1), max(d2), max(d3)]);

if isempty(mind) == 1 || isempty(maxd) == 1
    stop = -1;
    n = 3;
else
    stop = floor(log10(mind)) - 1;
    n = ceil(log10(maxd)) + 1;
end

if handles.auto_lim == 1
   handles.xmin = 10^stop;
   handles.xmax = 10^n;
else
end

if handles.fit_eq == 1
        if length(d1) >= 2
            x = lsqcurvefit(@(x,d1_mm) rosin(x,d1_mm),[1;1],d1_mm,data1);
            d1_f(1) = x(1)*((-1*log(99.9/100))^(1/x(2)));
            d1_f(2) = x(1)*((-1*log(0.1/100))^(1/x(2)));
        else
            d1_f = [];
        end
        
        if length(d2) >= 2
            x = lsqcurvefit(@(x,d2_mm) rosin(x,d2_mm),[1;1],d2_mm,data2);
            d2_f(1) = x(1)*((-1*log(99.9/100))^(1/x(2)));
            d2_f(2) = x(1)*((-1*log(0.1/100))^(1/x(2)));
        else
            d2_f = [];
        end
        
        if length(d3) >= 2
            x = lsqcurvefit(@(x,d3_mm) rosin(x,d3_mm),[1;1],d3_mm,data3);
            d3_f(1) = x(1)*((-1*log(99.9/100))^(1/x(2)));
            d3_f(2) = x(1)*((-1*log(0.1/100))^(1/x(2)));
        else
            d3_f = [];
        end
        
        if handles.x_axis_units == 1 %micron
            d1_f = d1_f.*1000;
            d2_f = d2_f.*1000;
            d3_f = d3_f.*1000;
        elseif handles.x_axis_units == 2 %mm
        elseif handles.x_axis_units == 3 %cm
            d1_f = d1_f./10;
            d2_f = d2_f./10;
            d3_f = d3_f./10;
        elseif handles.x_axis_units == 4 %dm
            d1_f = d1_f./100;
            d2_f = d2_f./100;
            d3_f = d3_f./100;
        elseif handles.x_axis_units == 5 %m
            d1_f = d1_f./1000;
            d2_f = d2_f./1000;
            d3_f = d3_f./1000; 
        end 
else
end

n=n-stop;

stop=[10^(stop), 10^(stop+n)];
stop=log10(stop);

xmin=log10(handles.xmin);
xmin=xmin-(min(stop));

xmax=log10(handles.xmax);
xmax=xmax-min(stop);

d1=log10(d1);
d1=d1-(min(stop));

d2=log10(d2);
d2=d2-(min(stop));

d3=log10(d3);
d3=d3-(min(stop));

stop=stop-min(stop);

xmin=xmin.*(n*1000/(max(stop)));
xmax=xmax.*(n*1000/(max(stop)));

d1=d1.*(n*1000/(max(stop)));
d2=d2.*(n*1000/(max(stop)));
d3=d3.*(n*1000/(max(stop)));

data1=log(log(100./data1));
%data1=data1+5.362;
%data1=data1.*(2000/6.0612);
%data1=2000-data1;

y3679 = log(log(100/(100/exp(1))));
%y3679 = y3679 + 5.362;
%y3679 = y3679*(2000/6.0612);
%y3679 = 2000 - y3679;

ymax = log(log(100/handles.ymax));
%ymax = ymax + 5.362;
%ymax = ymax*(2000/6.0612);
%ymax = 2000 - ymax;

ymin = log(log(100/handles.ymin));
%ymin = ymin + 5.362;
%ymin = ymin*(2000/6.0612);
%ymin = 2000 - ymin;

data2=log(log(100./data2));
%data2=data2+5.362;
%data2=data2.*(2000/6.0612);
%data2=2000-data2;

data3=log(log(100./data3));
%data3=data3+5.362;
%data3=data3.*(2000/6.0612);
%data3=2000-data3;

% Calculate linear fitting
if handles.fit_eq == 1;
    
else
        J=1;
        for I=1:80
            if isnan(handles.d1(I))==1 || isnan(handles.passing_1(I))==1
            else
                J=J+1;
            end
        end
        d1_f=[];
        if J>2
            fit=polyfit(d1,data1,1);
            d1_f(1)=xmin*fit(1)+fit(2);
            d1_f(2)=xmax*fit(1)+fit(2);
        end

        J=1;
        for I=1:80
            if isnan(handles.d2(I))==1 || isnan(handles.passing_2(I))==1
            else
                J=J+1;
            end
        end
        d2_f=[];
        if J>2
            fit=polyfit(d2,data2,1);
            d2_f(1)=xmin*fit(1)+fit(2);
            d2_f(2)=xmax*fit(1)+fit(2);
        else
        end

        J=1;
        for I=1:80
            if isnan(handles.d3(I))==1 || isnan(handles.passing_3(I))==1
            else
                J=J+1;
            end
        end
        d3_f=[];
        if J>2
            fit=polyfit(d3,data3,1);
            d3_f(1)=xmin*fit(1)+fit(2);
            d3_f(2)=xmax*fit(1)+fit(2);
        end
end

J=1;
d1=[];
for I=1:80
    if isnan(handles.d1(I))==1 || isnan(handles.passing_1(I))==1
    
    else
        d1(J) = handles.d1(I)*handles.units_1(I);
        J=J+1;
    end
end

J=1;
d2=[];
for I=1:80
    if isnan(handles.d2(I))==1 || isnan(handles.passing_2(I))==1
    
    else
        d2(J) = handles.d2(I)*handles.units_2(I);
        J=J+1;
    end
end

J=1;
d3=[];
for I=1:80
    if isnan(handles.d3(I))==1 || isnan(handles.passing_3(I))==1
    
    else
        d3(J) = handles.d3(I)*handles.units_3(I);
        J=J+1;
    end
end

if handles.x_axis_units == 1 %micron
    d1 = d1.*1000;
    d2 = d2.*1000;
    d3 = d3.*1000;
elseif handles.x_axis_units == 2 %mm
elseif handles.x_axis_units == 3 %cm
    d1 = d1./10;
    d2 = d2./10;
    d3 = d3./10;
elseif handles.x_axis_units == 4 %dm
    d1 = d1./100;
    d2 = d2./100;
    d3 = d3./100;
elseif handles.x_axis_units == 5 %m
    d1 = d1./1000;
    d2 = d2./1000;
    d3 = d3./1000; 
end

axes(handles.ax_hand);
cla reset;

legend off;
box on;

if handles.show1 == 1
   if isempty(d1_f)
       show1 = 0;
   else
       show1 = 1;
   end
else
    show1 = 0;
end

if handles.show2 == 1
   if isempty(d2_f)
       show2 = 0;
   else
       show2 = 1;
   end
else
    show2 = 0;
end

if handles.show3 == 1
   if isempty(d3_f)
       show3 = 0;
   else
       show3 = 1;
   end
else
    show3 = 0;
end

 if handles.points == 1 && handles.approx == 1
     if show1 == 1 && show2 == 0 && show3 == 0
         if handles.fit_eq == 1
             semilogx(d1, data1, 'ro', d1_f, [log(log(100/99.9)) log(log(100/0.01))], 'r--');
         else
            semilogx(d1, data1, 'ro', [handles.xmin handles.xmax], d1_f, 'r--');
         end
            if handles.legend == 1 
                    legend(handles.legend_text_1, handles.legend_text_2, 'Location', 'SouthEast');
            else
            end
     elseif show1 == 1 && show2 == 1 && show3 == 1
         if handles.fit_eq == 1
             semilogx(d1, data1, 'ro', d1_f, [log(log(100/99.9)) log(log(100/0.01))], 'r--', d2, data2, 'bx', d2_f, [log(log(100/99.9)) log(log(100/0.01))], 'b-.', d3, data3, 'g*', d3_f, [log(log(100/99.9)) log(log(100/0.01))], 'g:');
         else
            semilogx(d1, data1, 'ro', [handles.xmin handles.xmax], d1_f, 'r--', d2, data2, 'bx', [handles.xmin handles.xmax], d2_f, 'b-.', d3, data3, 'g*', [handles.xmin handles.xmax], d3_f, 'g:');
         end
            if handles.legend == 1 
                    legend(handles.legend_text_1, handles.legend_text_2, handles.legend_text_3, handles.legend_text_4, handles.legend_text_5, handles.legend_text_6, 'Location', 'SouthEast');
            else
            end
     elseif show1 == 1 && show2 == 1
         if handles.fit_eq == 1
             semilogx(d1, data1, 'ro', d1_f, [log(log(100/99.9)) log(log(100/0.01))], 'r--', d2, data2, 'bx', d2_f, [log(log(100/99.9)) log(log(100/0.01))], 'b-.');
         else
             semilogx(d1, data1, 'ro', [handles.xmin handles.xmax], d1_f, 'r--', d2, data2, 'bx', [handles.xmin handles.xmax], d2_f, 'b-.');
         end
             if handles.legend == 1 
                    legend(handles.legend_text_1, handles.legend_text_2, handles.legend_text_3, handles.legend_text_4, 'Location', 'SouthEast');
            else
            end
     elseif show1 == 1 && show3 == 1
         if handles.fit_eq == 1
             semilogx(d1, data1, 'ro', d1_f, [log(log(100/99.9)) log(log(100/0.01))], 'r--', d3, data3, 'g*', d3_f, [log(log(100/99.9)) log(log(100/0.01))], 'g:.');
         else
             semilogx(d1, data1, 'ro', [handles.xmin handles.xmax], d1_f, 'r--', d3, data3, 'g*', [handles.xmin handles.xmax], d3_f, 'g:.');
         end
             if handles.legend == 1 
                    legend(handles.legend_text_1, handles.legend_text_2, handles.legend_text_5, handles.legend_text_6, 'Location', 'SouthEast');
            else
            end
     elseif show2 == 1 && show3 == 1
         if handles.fit_eq == 1
            semilogx(d2, data2, 'bx', d2_f, [log(log(100/99.9)) log(log(100/0.01))], 'b-.', d3, data3, 'g*', d3_f, [log(log(100/99.9)) log(log(100/0.01))], 'g:'); 
         else
            semilogx(d2, data2, 'bx', [handles.xmin handles.xmax], d2_f, 'b-.', d3, data3, 'g*', [handles.xmin handles.xmax], d3_f, 'g:');
         end
            if handles.legend == 1 
                    legend(handles.legend_text_3, handles.legend_text_4, handles.legend_text_5, handles.legend_text_6, 'Location', 'SouthEast');
            else
            end
     elseif show2 == 1
         if handles.fit_eq == 1
             semilogx(d2, data2, 'bx', d2_f, [log(log(100/99.9)) log(log(100/0.01))], 'b-.');
         else
            semilogx(d2, data2, 'bx', [handles.xmin handles.xmax], d2_f, 'b-.');
         end
            if handles.legend == 1 
                    legend(handles.legend_text_3, handles.legend_text_4, 'Location', 'SouthEast');
            else
            end
     elseif show3 == 1
         if handles.fit_eq == 1
             semilogx(d3, data3, 'g*', d3_f, [log(log(100/99.9)) log(log(100/0.01))], 'g:');
         else
            semilogx(d3, data3, 'g*', [handles.xmin handles.xmax], d3_f, 'g:');
         end
            if handles.legend == 1 
                    legend(handles.legend_text_5, handles.legend_text_6, 'Location', 'SouthEast');
            else
            end
     else
         
     end
               
 elseif handles.points == 1
    if show1 == 1 && show2 == 0 && show3 == 0
        semilogx(d1, data1, 'ro');
        if handles.legend == 1 
                legend(handles.legend_text_1, 'Location', 'SouthEast');
        else
        end
     elseif show1 == 1 && show2 == 1 && show3 == 1
        semilogx(d1, data1, 'ro', d2, data2, 'bx', d3, data3, 'g*');
        if handles.legend == 1 
                legend(handles.legend_text_1, handles.legend_text_3, handles.legend_text_5, 'Location', 'SouthEast');
        else
        end
     elseif show1 == 1 && show2 == 1
         semilogx(d1, data1, 'ro', d2, data2, 'bx');
        if handles.legend == 1 
                legend(handles.legend_text_1, handles.legend_text_3, 'Location', 'SouthEast');
        else
        end
     elseif show1 == 1 && show3 == 1
         semilogx(d1, data1, 'ro', d3, data3, 'g*');
        if handles.legend == 1 
                legend(handles.legend_text_1, handles.legend_text_5, 'Location', 'SouthEast');
        else
        end
     elseif show2 == 1 && show3 == 1
        semilogx(d2, data2, 'bx', d3, data3, 'g*');
        if handles.legend == 1 
                legend(handles.legend_text_3, handles.legend_text_5, 'Location', 'SouthEast');
        else
        end
     elseif show2 == 1
        semilogx(d2, data2, 'bx');
        if handles.legend == 1 
                legend(handles.legend_text_3, 'Location', 'SouthEast');
        else
        end
     elseif show3 == 1
        semilogx(d3, data3, 'g*');
        if handles.legend == 1 
                legend(handles.legend_text_5, 'Location', 'SouthEast');
        else
        end
     else
         
     end
       
 elseif handles.approx == 1
    if show1 == 1 && show2 == 0 && show3 == 0
        if handles.fit_eq == 1
            semilogx(d1_f, [log(log(100/99.9)) log(log(100/0.01))], 'r--');
        else
            semilogx([handles.xmin handles.xmax], d1_f, 'r--');
        end
        if handles.legend == 1 
                legend(handles.legend_text_2, 'Location', 'SouthEast');
        else
        end
     elseif show1 == 1 && show2 == 1 && show3 == 1
         if handles.fit_eq == 1
            semilogx(d1_f, [log(log(100/99.9)) log(log(100/0.01))], 'r--', d2_f, [log(log(100/99.9)) log(log(100/0.01))], 'b-.', d3_f, [log(log(100/99.9)) log(log(100/0.01))], 'g:');
         else
            semilogx([handles.xmin handles.xmax], d1_f, 'r--', [handles.xmin handles.xmax], d2_f, 'b-.', [handles.xmin handles.xmax], d3_f, 'g:');
         end
        if handles.legend == 1 
                legend(handles.legend_text_2, handles.legend_text_4, handles.legend_text_6, 'Location', 'SouthEast');
        else
        end
     elseif show1 == 1 && show2 == 1
         if handles.fit_eq == 1
            semilogx(d1_f, [log(log(100/99.9)) log(log(100/0.01))], 'r--', d2_f, [log(log(100/99.9)) log(log(100/0.01))], 'b-.');
         else
            semilogx([handles.xmin handles.xmax], d1_f, 'r--', [handles.xmin handles.xmax], d2_f, 'b-.');
         end
        if handles.legend == 1 
                legend(handles.legend_text_2, handles.legend_text_4, 'Location', 'SouthEast');
        else
        end
     elseif show1 == 1 && show3 == 1
         if handles.fit_eq == 1
            semilogx(d1_f, [log(log(100/99.9)) log(log(100/0.01))], 'r--', d3_f, [log(log(100/99.9)) log(log(100/0.01))], 'g:.'); 
         else
            semilogx([handles.xmin handles.xmax], d1_f, 'r--', [handles.xmin handles.xmax], d3_f, 'g:.');
         end
        if handles.legend == 1 
                legend(handles.legend_text_2, handles.legend_text_6, 'Location', 'SouthEast');
        else
        end
     elseif show2 == 1 && show3 == 1
         if handles.fit_eq == 1
            semilogx(d2_f, [log(log(100/99.9)) log(log(100/0.01))], 'b-.', d3_f, [log(log(100/99.9)) log(log(100/0.01))], 'g:');
         else
            semilogx([handles.xmin handles.xmax], d2_f, 'b-.', [handles.xmin handles.xmax], d3_f, 'g:');
         end
        if handles.legend == 1 
                legend(handles.legend_text_4, handles.legend_text_6, 'Location', 'SouthEast');
        else
        end
     elseif show2 == 1
         if handles.fit_eq == 1
            semilogx(d2_f, [log(log(100/99.9)) log(log(100/0.01))], 'b-.');
         else
            semilogx([handles.xmin handles.xmax], d2_f, 'b-.');
         end
        if handles.legend == 1 
                legend(handles.legend_text_4, 'Location', 'SouthEast');
        else
        end
     elseif show3 == 1
         if handles.fit_eq == 1
            semilogx(d3_f, [log(log(100/99.9)) log(log(100/0.01))], 'g:');
         else
            semilogx([handles.xmin handles.xmax], d3_f, 'g:');
         end
        if handles.legend == 1 
                legend(handles.legend_text_6, 'Location', 'SouthEast');
        else
        end
     else
         
     end
                
 else
        set(gca, 'XScale', 'log');
       
 end
 
    ytck = handles.ytck;
    ytck = log10(log10(100./ytck));
    ytck = ytck + abs(min(ytck));
    ytck = ytck.*(2000/max(ytck));
    ytck = 2000 - ytck;
    set(gca,'ytick',ytck);
    
    if handles.y_axis == 1
        set(gca,'yticklabel',handles.ytck_label);
    else
        set(gca,'yticklabel',handles.ytck_label2);
    end
    
    set(gca,'YDir','reverse');
    
    if handles.line_3679 == 1
        line([handles.xmin handles.xmax], [y3679 y3679], 'LineStyle','-', 'Color', 'k');
    else
    end
        
    xlabel(handles.xlabel_text, 'FontWeight', 'Bold');
    ylabel(handles.ylabel_text, 'FontWeight', 'Bold');
    title(handles.title_text, 'FontWeight', 'Bold', 'FontSize', 14);
    
    if handles.zoom == 1
        ylim([handles.yminz handles.ymaxz]);
        xlim([handles.xminz handles.xmaxz]);
    else
        ylim([ymin-1 ymax]);
        xlim([handles.xmin handles.xmax]);
    end
        
        
    if handles.grid == 1
        grid on;
    else
       grid off; 
    end


% --------------------------------------------------------------------
function Menu_File_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_File (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
guidata(hObject, handles);


% --- Executes when user attempts to close figure_RRD.
function figure_RRD_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure_RRD (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
delete(hObject);


% --------------------------------------------------------------------
function Menu_Edit_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_Edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Menu_Data_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_Data (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Menu_Calculate_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_Calculate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Menu_help_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_help (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Menu_Instructions_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_Instructions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA);
web('html/Rosin_Rammler.html');

% --------------------------------------------------------------------
function Menu_About_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_About (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
about();

% --------------------------------------------------------------------
function Menu_Size_distr_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_Size_distr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
calculator();


% --------------------------------------------------------------------
function menu_graph_properties_Callback(hObject, eventdata, handles)
% hObject    handle to menu_graph_properties (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
edit_RRD();

% --------------------------------------------------------------------
function menu_save_graph_Callback(hObject, eventdata, handles)
% hObject    handle to menu_save_graph (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
figure('Name','RR Diagram','NumberTitle','on');
handles.ax_hand = gca;
[handles] = refresh_table(handles);
[handles] = plot_RR(handles);
guidata(hObject, handles);

% --------------------------------------------------------------------
function menu_save_expdata_Callback(hObject, eventdata, handles)
% hObject    handle to menu_save_expdata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
saveData=cell(81,5);
saveData(1, 1:5) = {'#', 'Mesh size d', 'Units', 'Passing [%%]', 'Retained [%%]'};

if handles.data_actual == 1
    for I=1:80
        if handles.units_1(I) == 1/1000
           saveData(I+1, 3) = {'micron'};
        elseif handles.units_1(I) == 1
           saveData(I+1, 3) = {'mm'};
        elseif handles.units_1(I) == 10
           saveData(I+1, 3) = {'cm'};
        elseif handles.units_1(I) == 100
           saveData(I+1, 3) = {'dm'};
        elseif handles.units_1(I) == 1000
           saveData(I+1, 3) = {'m'};
        end

        saveData(I+1, 1) = {I};

        if isnan(handles.d1(I))==1
            saveData(I+1, 2) = {[]};
        else
            saveData(I+1, 2) = {handles.d1(I)};
        end

        if isnan(handles.passing_1(I)) == 1 || isnan(handles.retained_1(I)) == 1
            saveData(I+1, 4) = {[]};
            saveData(I+1, 5) = {[]};
        else
            saveData(I+1, 4) = {handles.passing_1(I)};
            saveData(I+1, 5) = {handles.retained_1(I)};
        end
    end
elseif handles.data_actual == 2;
    for I=1:80
        if handles.units_2(I) == 1/1000
           saveData(I+1, 3) = {'micron'};
        elseif handles.units_2(I) == 1
           saveData(I+1, 3) = {'mm'};
        elseif handles.units_2(I) == 10
           saveData(I+1, 3) = {'cm'};
        elseif handles.units_2(I) == 100
           saveData(I+1, 3) = {'dm'};
        elseif handles.units_2(I) == 1000
           saveData(I+1, 3) = {'m'};
        end

        saveData(I+1, 1) = {I};

        if isnan(handles.d2(I))==1
            saveData(I+1, 2) = {[]};
        else
            saveData(I+1, 2) = {handles.d2(I)};
        end

        if isnan(handles.passing_2(I)) == 1 || isnan(handles.retained_2(I)) == 1
            saveData(I+1, 4) = {[]};
            saveData(I+1, 5) = {[]};
        else
            saveData(I+1, 4) = {handles.passing_2(I)};
            saveData(I+1, 5) = {handles.retained_2(I)};
        end
    end
else
    for I=1:80
        if handles.units_3(I) == 1/1000
           saveData(I+1, 3) = {'micron'};
        elseif handles.units_3(I) == 1
           saveData(I+1, 3) = {'mm'};
        elseif handles.units_3(I) == 10
           saveData(I+1, 3) = {'cm'};
        elseif handles.units_3(I) == 100
           saveData(I+1, 3) = {'dm'};
        elseif handles.units_3(I) == 1000
           saveData(I+1, 3) = {'m'};
        end

        saveData(I+1, 1) = {I};

        if isnan(handles.d3(I))==1
            saveData(I+1, 2) = {[]};
        else
            saveData(I+1, 2) = {handles.d3(I)};
        end

        if isnan(handles.passing_3(I)) == 1 || isnan(handles.retained_3(I)) == 1
            saveData(I+1, 4) = {[]};
            saveData(I+1, 5) = {[]};
        else
            saveData(I+1, 4) = {handles.passing_3(I)};
            saveData(I+1, 5) = {handles.retained_3(I)};
        end
    end
end

[filename, pathname] = uiputfile( ...
       {'*.csv';'*.xls';'*.txt'; '*.dat'}, ...
        'Save as');
if isnumeric(filename) == 1 || isnumeric(pathname) == 1
    
else
    if strcmp(filename(length(filename)), 'v')
        cell2csv([pathname filename], saveData, ',', '2003');
    elseif strcmp(filename(length(filename)), 's')
        answer = inputdlg('Specify sheet number for saving data (numerical value):','Select Excel Sheet', 1, {'1'});
        sheet = str2double(answer);
        
        if isnan(sheet) == 1
            uiwait(errordlg('Sheet number is not a valid numerical value. Breaking!','Error'));
            success = 1;
        else    
            [success, message] = xlswrite([pathname filename], saveData, sheet, 'A2:E82');
        end
        
        if success == 0
            uiwait(errordlg(struct2cell(message), 'Error', 'Modal'));
        end
    else
        dlmcell([pathname filename], saveData);
    end
end


% --------------------------------------------------------------------
function menu_load_expdata_Callback(hObject, eventdata, handles)
% hObject    handle to menu_load_expdata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile('*.csv; *.xls; *.txt; *.dat', 'Load size distribution data...');
if isequal(filename,0) || isequal(pathname,0)
       
else
    if strcmp(filename(length(filename)), 'v') == 1 %csv
        res = csv2cell([pathname filename], ',');
        no = cell2num(res(2:81, 1));
        mesh = cell2num(res(2:81, 2));
        unit = res(2:81, 3);
        pass = cell2num(res(2:81, 4));
        ret = cell2num(res(2:81, 5));
    elseif strcmp(filename(length(filename)), 's') == 1 %xls
        [~,sheets] = xlsfinfo([pathname filename]);
        if length(sheets) > 1
            answer = inputdlg('Excel file you selected contains more than one sheet. Please select sheet from which to load data (numerical value):','Select Excel Sheet', 1, {'1'});
            sheet = str2double(answer);
            if sheet > length(sheets)
                uiwait(warndlg('You have selected not-existing sheet, loading from first sheet instead.','Warning'));
                sheet = 1;
            end
            
            [~, ~, raw] = xlsread([pathname filename], sheet, 'A3:E82');
        else
            [~, ~, raw] = xlsread([pathname filename], 1, 'A3:E82');
        end
        no = cell2num(raw(1:80, 1));
        mesh = cell2num(raw(1:80, 2));
        unit = raw(1:80, 3);
        pass = cell2num(raw(1:80, 4));
        ret = cell2num(raw(1:80, 5));
    else %txt, dat
        [no mesh unit pass ret] = textread([pathname filename],'%d %f %s %f %f','headerlines',1,'delimiter','\t', 'emptyvalue', NaN);
    end
    
    % Check loaded data
    x(1:80, 1) = 1:80;
    units = ones(80, 1);
    if sum(no == x)==80 && sum(strcmp(unit(1), {'micron', 'mm', 'cm', 'dm', 'm'}))==1 && sum(strcmp(unit(80), {'micron', 'mm', 'cm', 'dm', 'm'}))==1
        for I=1:80 %Change units to numbers
            if strcmp(unit(I), 'mm') == 1
                units(I) = 1;
            elseif strcmp(unit(I), 'micron') == 1
                units(I) = 1/1000;
            elseif strcmp(unit(I), 'cm') == 1
                units(I) = 10;
            elseif strcmp(unit(I), 'dm') == 1
                units(I) = 100;
            else
                units(I) = 1000;
            end
        end
        
        % Get rid of negative values
        mesh = abs(mesh);
        pass = abs(pass);
        ret = abs(ret);
        
        for I=1:80
            if isnan(pass(I)) == 1 && isnan(ret(I)) == 1
            elseif isnan(pass(I)) == 1
                if ret(I) >= 100
                    ret(I) = 99.9;
                    pass(I) = 100 - ret(I);
                elseif ret(I) == 0
                    ret(I) = 0.1;
                    pass(I) = 100 - ret(I);
                else
                    pass(I) = 100 - ret(I);
                end    
            elseif isnan(ret(I)) == 1
                if pass(I) >= 100
                    pass(I) = 99.9;
                    ret(I) = 100 - pass(I);
                elseif pass(I) == 0
                    pass(I) = 0.1;
                    ret(I) = 100 - pass(I);
                else
                    pass(I) = 100 - ret(I);
                end    
            else
                if pass(I) == 0
                    pass(I) = 0.1;
                elseif pass(I) >= 100
                    pass(I) = 99.9;
                end
                
                if ret(I) == 0
                    ret(I) = 0.1;
                elseif ret(I) >= 100
                    ret(I) = 99.9;
                end
                
                if ret(I) + pass(I) == 100
                    
                else
                    pass(I) = 100 - ret(I);
                end    
            end
        end
        
        % Forward loaded data to handles
        if handles.data_actual == 1
            handles.d1 = mesh;
            handles.units_1 = units;
            handles.passing_1 = pass;
            handles.retained_1 = ret;
        elseif handles.data_actual == 2
            handles.d2 = mesh;
            handles.units_2 = units;
            handles.passing_2 = pass;
            handles.retained_2 = ret;
        else
            handles.d3 = mesh;
            handles.units_3 = units;
            handles.passing_3 = pass;
            handles.retained_3 = ret;
        end
        [handles] = refresh_table(handles);
    else
        uiwait(errordlg('Inconsistent data', 'Error', 'Modal'));
    end
end
guidata(hObject, handles);



% --------------------------------------------------------------------
function Menu_3679_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_3679 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.line_3679 == 1
    set(hObject, 'Checked', 'off');
    handles.line_3679 = 0;
else
    set(hObject, 'Checked', 'on');
    handles.line_3679 = 1;
end
guidata(hObject, handles);


% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to uitable1 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)

tableData = get(handles.uitable1,'data');
stop = 0;
if eventdata.Indices(2) == 1 || eventdata.Indices(2) == 3 || eventdata.Indices(2) == 4 || eventdata.Indices(2) == 5
   
    if isempty(eventdata.EditData) == 1
        if eventdata.Indices(2) == 1 % Mesh Size
            if handles.data_actual == 1
               handles.d1(eventdata.Indices(1)) = NaN;
            elseif handles.data_actual == 2
               handles.d2(eventdata.Indices(1)) = NaN;
            else
               handles.d3(eventdata.Indices(1)) = NaN;
            end
        elseif eventdata.Indices(2) == 3 || eventdata.Indices(2) == 4 % Passing or Retained
            if handles.data_actual == 1
               handles.passing_1(eventdata.Indices(1)) = NaN;
               handles.retained_1(eventdata.Indices(1)) = NaN;
            elseif handles.data_actual == 2
               handles.passing_2(eventdata.Indices(1)) = NaN;
               handles.retained_2(eventdata.Indices(1)) = NaN;
            else
               handles.passing_3(eventdata.Indices(1)) = NaN;
               handles.retained_3(eventdata.Indices(1)) = NaN;
            end
        end
        stop = 1;
    elseif isnan(str2double(eventdata.EditData)) == 1
        uiwait(errordlg('Please enter numerical values only.', 'Error', 'Modal'));
        tableData(eventdata.Indices(1),eventdata.Indices(2)) = {eventdata.PreviousData};
        set(handles.uitable1, 'data', tableData);
        stop = 1;
    elseif str2double(eventdata.EditData) < 0
        uiwait(errordlg('Please enter non-negative values only.', 'Error', 'Modal'));
        tableData(eventdata.Indices(1),eventdata.Indices(2)) = {eventdata.PreviousData};
        set(handles.uitable1, 'data', tableData);
        stop = 1;
    end
end

if stop == 0
    if eventdata.Indices(2) == 1 % Mesh Size
        if str2double(eventdata.EditData)  == 0
            uiwait(errordlg('Please do not use mesh size 0.', 'Error', 'Modal'));
            tableData(eventdata.Indices(1),eventdata.Indices(2)) = {eventdata.PreviousData};
            set(handles.uitable1, 'data', tableData);
        else
            if handles.data_actual == 1
                handles.d1(eventdata.Indices(1)) = str2double(eventdata.EditData);
            elseif handles.data_actual == 2
                handles.d2(eventdata.Indices(1)) = str2double(eventdata.EditData);
            else
                handles.d3(eventdata.Indices(1)) = str2double(eventdata.EditData);
            end
        end
    elseif eventdata.Indices(2) == 2 % Units
        if strcmp(eventdata.EditData,'micron') == 1
            if handles.data_actual == 1
                handles.units_1(eventdata.Indices(1)) = 1/1000;
            elseif handles.data_actual == 2
                handles.units_2(eventdata.Indices(1)) = 1/1000;
            elseif handles.data_actual == 2
                handles.units_3(eventdata.Indices(1)) = 1/1000;
            end
        elseif strcmp(eventdata.EditData, 'mm') == 1
            if handles.data_actual == 1
                handles.units_1(eventdata.Indices(1)) = 1;
            elseif handles.data_actual == 2
                handles.units_2(eventdata.Indices(1)) = 1;
            elseif handles.data_actual == 2
                handles.units_3(eventdata.Indices(1)) = 1;
            end
        elseif strcmp(eventdata.EditData, 'cm') == 1
            if handles.data_actual == 1
                handles.units_1(eventdata.Indices(1)) = 10;
            elseif handles.data_actual == 2
                handles.units_2(eventdata.Indices(1)) = 10;
            elseif handles.data_actual == 2
                handles.units_3(eventdata.Indices(1)) = 10;
            end 
        elseif strcmp(eventdata.EditData, 'dm') == 1
            if handles.data_actual == 1
                handles.units_1(eventdata.Indices(1)) = 100;
            elseif handles.data_actual == 2
                handles.units_2(eventdata.Indices(1)) = 100;
            elseif handles.data_actual == 2
                handles.units_3(eventdata.Indices(1)) = 100;
            end 
        elseif strcmp(eventdata.EditData, 'm') == 1 
            if handles.data_actual == 1
                handles.units_1(eventdata.Indices(1)) = 1000;
            elseif handles.data_actual == 2
                handles.units_2(eventdata.Indices(1)) = 1000;
            elseif handles.data_actual == 2
                handles.units_3(eventdata.Indices(1)) = 1000;
            end 
        end
    elseif eventdata.Indices(2) == 3 % Passing
        if str2double(eventdata.EditData)  == 0 || str2double(eventdata.EditData)  >= 100 
            uiwait(errordlg('Material streams for Rosin-Rammler Diagram must be between 0 and 100 % (excluding).', 'Error', 'Modal'));
            tableData(eventdata.Indices(1),eventdata.Indices(2)) = {eventdata.PreviousData};
            set(handles.uitable1, 'data', tableData);
        else
            if handles.data_actual == 1
                handles.passing_1(eventdata.Indices(1)) = str2double(eventdata.EditData);
                handles.retained_1(eventdata.Indices(1)) = 100 - str2double(eventdata.EditData);
            elseif handles.data_actual == 2
                handles.passing_2(eventdata.Indices(1)) = str2double(eventdata.EditData);
                handles.retained_2(eventdata.Indices(1)) = 100 - str2double(eventdata.EditData);
            else
                handles.passing_3(eventdata.Indices(1)) = str2double(eventdata.EditData);
                handles.retained_3(eventdata.Indices(1)) = 100 - str2double(eventdata.EditData);
            end
        end
    elseif eventdata.Indices(2) == 4 % Retained
        if str2double(eventdata.EditData)  == 0 || str2double(eventdata.EditData)  >= 100 
            uiwait(errordlg('Material streams for Rosin-Rammler Diagram must be between 0 and 100 % (excluding).', 'Error', 'Modal'));
            tableData(eventdata.Indices(1),eventdata.Indices(2)) = {eventdata.PreviousData};
            set(handles.uitable1, 'data', tableData);
        else
            if handles.data_actual == 1
                handles.retained_1(eventdata.Indices(1)) = str2double(eventdata.EditData);
                handles.passing_1(eventdata.Indices(1)) = 100 - str2double(eventdata.EditData);
            elseif handles.data_actual == 2
                handles.retained_2(eventdata.Indices(1)) = str2double(eventdata.EditData);
                handles.passing_2(eventdata.Indices(1)) = 100 - str2double(eventdata.EditData);
            else
                handles.retained_3(eventdata.Indices(1)) = str2double(eventdata.EditData);
                handles.passing_3(eventdata.Indices(1)) = 100 - str2double(eventdata.EditData);
            end
        end
    end
end
guidata(hObject, handles);


% --------------------------------------------------------------------
function Untitled_12_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(hObject, 'Checked', 'on');
    handles.data_actual = 3;
set(handles.Untitled_10, 'Checked', 'off');
set(handles.Untitled_11, 'Checked', 'off');
[handles] = refresh_table(handles);
guidata(hObject, handles);

% --------------------------------------------------------------------
function Untitled_11_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(hObject, 'Checked', 'on');
    handles.data_actual = 2;
set(handles.Untitled_12, 'Checked', 'off');
set(handles.Untitled_10, 'Checked', 'off');
[handles] = refresh_table(handles);
guidata(hObject, handles);

% --------------------------------------------------------------------
function Untitled_10_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(hObject, 'Checked', 'on');
    handles.data_actual = 1;
set(handles.Untitled_12, 'Checked', 'off');
set(handles.Untitled_11, 'Checked', 'off');
[handles] = refresh_table(handles);
guidata(hObject, handles);

function [handles] = refresh_table(handles)
tableData = get(handles.uitable1,'data');
if handles.data_actual == 1
    passing_1 = cell(1,80);
    retained_1 = cell(1,80);
    d1 = cell(1,80);
    
     for I=1:80
        if isnan(handles.d1(I))==1
            d1(I) = {''};
        else
            d1(I) = num2cell(handles.d1(I)); 
        end
        
        if isnan(handles.passing_1(I))==1
            passing_1(I) = {''};
            retained_1(I) = {''};
        else
            passing_1(I) = num2cell(handles.passing_1(I)); 
            retained_1(I) = num2cell(handles.retained_1(I)); 
        end
            
        if handles.units_1(I) == 1/1000
            tableData(I, 2) = {'micron'};
        elseif handles.units_1(I) == 1
            tableData(I, 2) = {'mm'};
        elseif handles.units_1(I) == 10
            tableData(I, 2) = {'cm'};
        elseif handles.units_1(I) == 100
            tableData(I, 2) = {'dm'};
        elseif handles.units_1(I) == 1000
            tableData(I, 2) = {'m'};
        end
    end
    tableData(1:80,1) = d1;
    tableData(1:80,4) = retained_1;
    tableData(1:80,3) = passing_1;
    
elseif handles.data_actual == 2
    passing_2 = cell(1,80);
    retained_2 = cell(1,80);
    d2 = cell(1,80);
    
     for I=1:80
        if isnan(handles.d2(I))==1
            d2(I) = {''};
        else
            d2(I) = num2cell(handles.d2(I)); 
        end
        
        if isnan(handles.passing_2(I))==1
            passing_2(I) = {''};
            retained_2(I) = {''};
        else
            passing_2(I) = num2cell(handles.passing_2(I)); 
            retained_2(I) = num2cell(handles.retained_2(I)); 
        end
            
        if handles.units_2(I) == 1/1000
            tableData(I, 2) = {'micron'};
        elseif handles.units_2(I) == 1
            tableData(I, 2) = {'mm'};
        elseif handles.units_2(I) == 10
            tableData(I, 2) = {'cm'};
        elseif handles.units_2(I) == 100
            tableData(I, 2) = {'dm'};
        elseif handles.units_2(I) == 1000
            tableData(I, 2) = {'m'};
        end
    end
    tableData(1:80,1) = d2;
    tableData(1:80,4) = retained_2;
    tableData(1:80,3) = passing_2;
else
    passing_3 = cell(1,80);
    retained_3 = cell(1,80);
    d3 = cell(1,80);
    
     for I=1:80
        if isnan(handles.d3(I))==1
            d3(I) = {''};
        else
            d3(I) = num2cell(handles.d3(I)); 
        end
        
        if isnan(handles.passing_3(I))==1
            passing_3(I) = {''};
            retained_3(I) = {''};
        else
            passing_3(I) = num2cell(handles.passing_3(I)); 
            retained_3(I) = num2cell(handles.retained_3(I)); 
        end
            
        if handles.units_3(I) == 1/1000
            tableData(I, 2) = {'micron'};
        elseif handles.units_3(I) == 1
            tableData(I, 2) = {'mm'};
        elseif handles.units_3(I) == 10
            tableData(I, 2) = {'cm'};
        elseif handles.units_3(I) == 100
            tableData(I, 2) = {'dm'};
        elseif handles.units_3(I) == 1000
            tableData(I, 2) = {'m'};
        end
    end
    tableData(1:80,1) = d3;
    tableData(1:80,4) = retained_3;
    tableData(1:80,3) = passing_3;
end
set(handles.uitable1, 'data', tableData);


% --- Executes during object creation, after setting all properties.
function checkbox_grid_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox_grid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
set(hObject,'Value',1)
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function checkbox_points_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox_points (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
set(hObject,'Value',0)
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function checkbox_approx_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox_approx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
set(hObject,'Value',0)
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function checkbox_legend_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox_legend (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --------------------------------------------------------------------
function fit_eq_Callback(hObject, eventdata, handles)
% hObject    handle to fit_eq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.fit_eq == 1;
    set(hObject, 'Checked', 'off');
    handles.fit_eq = 0;
else
    if exist('lsqcurvefit') == 2;
        set(hObject, 'Checked', 'on');
        handles.fit_eq = 1;
    else
        warndlg('Optimization Toolbox must be installed in order to use equation fitting routine, canceling.','!! Warning !!')
    end
    
end
guidata(hObject, handles);

function F = rosin(x,d_data)
%ROSIN Summary of this function goes here
%   Detailed explanation goes here
F=100.*exp(-1.*((d_data./x(1)).^x(2)));


% --------------------------------------------------------------------
function Untitled_13_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.y_axis == 1;
    set(hObject, 'Checked', 'on');
    handles.y_axis = 0;
else
    set(hObject, 'Checked', 'off');
    handles.y_axis = 1;
end

helpdlg('Replot graph to apply new settings and note that Y-axis label does not change automatically. Change it manually by editing graph properties if necessary.','Information')

guidata(hObject, handles);

Contact us