Code covered by the BSD License  

Highlights from
Laboratory froth flotation machine automation GUI

image thumbnail

Laboratory froth flotation machine automation GUI

by

 

This program was designed to work as a controller for upgraded DV-2 laboratory flotation machine.

main.m
function varargout = main(varargin)
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
%               FLOTATION MACHINE CONTROLER
%
%                    Ing. Ivan Brezni
%                  ivan.brezani@tuke.sk
%
% Ivan Brezani
% Institute of Montanneous Sciences and Environmental Protection
% Faculty of Mining, Ecology, Process Control and Geotechnology
% Technical University of Kosice
%
% September 2011
%
% Necessary PC hardware: advantech PCI-1713 (32 channel AI card configured
%                        for 16 differential channels) 
%                        advantech PCI-1720U (4 channel AO card)
%
% Other devices: PHP-701 (pH controller with 4-20 mA output)
%                FMA-A2409 (air flow controller with 4-20 mA output
%                and 0-5 V input)
%                MVXF50 (frequency regulator for AC drives with 0-10 V
%                output and 0-10 V input)
%                TXDIN70 (temperature transmitter with 4-20 mA output)
%
% Measure and log: pH (channel 0 AI)
%                  air flow (channel 1 AI)
%                  temperature (channel 2 AI)
%
% Set: stiring speed (channel 0 AO)
%      air flow (channel 1 AO)
%
% Resistors are used to convert 4-20 mA current signal to voltage.
% Example:  240 Ohm resistor converts 4-20 mA signal to 0.96-4.8 V signal
%           250 Ohm resistor converts 4-20 mA signal to 1-5 V signal
%
% AI channels 0-2 are set to 0-5 V
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

% Last Modified by GUIDE v2.5 21-Jan-2011 12:26:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @main_OpeningFcn, ...
                   'gui_OutputFcn',  @main_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 main is made visible.
function main_OpeningFcn(hObject, ~, 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 main (see VARARGIN)

% Choose default command line output for main
handles.output = hObject;

    handles.ai_card = 0; % 'advantech'
    handles.ao_card = 1; % 'advantech'

    handles.pH_ai_ch = 0; % Defaul channel for reading pH
    handles.lpm_ai_ch = 1; % Defaul channel for reading air flow
    handles.temp_ai_ch = 2; % Default channel for reading temperature
    handles.rpm_ai_ch = 3; % Default channel for reading stiring speed

    handles.rpm_ao_ch = 0; % Default channel for setting air flow 
    handles.lpm_ao_ch = 1; % Default channel for setting stiring speed
    
    refresh_axes(handles);
    drawnow
    
    handles.set = [];
    %handles.data = [];
    
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes main wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = main_OutputFcn(~, ~, 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 text11_CreateFcn(hObject, ~, handles) %#ok<*DEFNU>
% hObject    handle to text11 (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 text13_CreateFcn(hObject, ~, handles)
% hObject    handle to text13 (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 axes1_CreateFcn(hObject, ~, 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

% Hint: place code in OpeningFcn to populate axes1
    handles.pH_min_graph = 7;
    handles.pH_max_graph = 9.5;
    set(hObject, 'YLim', [handles.pH_min_graph handles.pH_max_graph]);
    
    % Periods:
    % -30 -   0 s = period 0  (60 s) /conditioning (stiring, no air flow)
    %   0 -  15 s = period 1  (15 s)
    %  15 -  45 s = period 2  (30 s)
    %  45 -  90 s = period 3  (45 s)
    %  90 - 120 s = period 4  (60 s)
    % 120 - 240 s = period 5 (120 s)
    % 240 - 480 s = period 6 (240 s) /if necessary

    % Names
    handles.period_name(1) = {'condit.'};
    handles.period_name(2) = {'period 1'};
    handles.period_name(3) = {'period 2'};
    handles.period_name(4) = {'period 3'};
    handles.period_name(5) = {'period 4'};
    handles.period_name(6) = {'period 5'};
    handles.period_name(7) = {'period 6'};
    handles.period_name(8) = {'period 7'};
    handles.period_name(9) = {'period 8'};
    handles.period_name(10) = {'period 9'};
    handles.period_name(11) = {'period 10'};

    % Times
    handles.period_time(1) = 5;
    handles.period_time(2) = 5;
    handles.period_time(3) = 5;
    handles.period_time(4) = 45;
    handles.period_time(5) = 60;
    handles.period_time(6) = 120;
    handles.period_time(7) = 240;
    handles.period_time(8) = 240;
    handles.period_time(9) = 240;
    handles.period_time(10) = 240;
    handles.period_time(11) = 240;

    % Use period
    handles.period_use(1) = 1;
    handles.period_use(2) = 1;
    handles.period_use(3) = 1;
    handles.period_use(4) = 0;
    handles.period_use(5) = 0;
    handles.period_use(6) = 0;
    handles.period_use(7) = 0;
    handles.period_use(8) = 0;
    handles.period_use(9) = 0;
    handles.period_use(10) = 0;
    handles.period_use(11) = 0;
    
    % Set max and min values (equal to 0 - 5 V)
    handles.pH_min = 0;
    handles.pH_max = 14;
    handles.pH_ohm = 240;
    handles.lpm_min = 0;
    handles.lpm_max = 10;
    handles.lpm_ohm = 240;
    handles.rpm_min = 0;
    handles.rpm_max = 1725;
    handles.temp_min = 10;
    handles.temp_max = 50;
    handles.temp_ohm = 240;
    
    handles.default_save_path = 'D:\Experiments';
    
guidata(hObject, handles)

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

% Hint: place code in OpeningFcn to populate axes2

    handles.lpm_val = 3;
    handles.lpm_min_graph = 0;
    handles.lpm_max_graph = 5;
    set(hObject, 'YLim', [handles.lpm_min_graph handles.lpm_max_graph]);
    
guidata(hObject, handles)

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

% Hint: place code in OpeningFcn to populate axes3
    handles.temp_min_graph = 18;
    handles.temp_max_graph = 22;
    set(hObject, 'YLim', [handles.temp_min_graph handles.temp_max_graph]);
    
guidata(hObject, handles)

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

% Hint: place code in OpeningFcn to populate axes4

    handles.rpm_val = 1200;
    handles.rpm_min_graph = 600;
    handles.rpm_max_graph = 1800;
    set(hObject, 'YLim', [handles.rpm_min_graph handles.rpm_max_graph]);
    
guidata(hObject, handles)


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

if strcmp(get(hObject, 'String'), 'START') == 1;

    set(hObject, 'String', 'STOP');

    % Create output channels
    handles.ao = analogoutput('advantech', handles.ao_card);
    addchannel(handles.ao, handles.rpm_ao_ch);
    addchannel(handles.ao, handles.lpm_ao_ch);
    set(handles.ao.Channel(1),'OutputRange',[0 10]);
    set(handles.ao.Channel(2),'OutputRange',[0 5]);
    set(handles.ao,'SampleRate',100);

    % Create input channels
    handles.ai = analoginput('advantech', handles.ai_card);
    addchannel(handles.ai, handles.pH_ai_ch);
    set(handles.ai.Channel(1),'InputRange',[0 5]);
    addchannel(handles.ai, handles.lpm_ai_ch);
    set(handles.ai.Channel(2),'InputRange',[0 5]);
    addchannel(handles.ai, handles.temp_ai_ch);
    set(handles.ai.Channel(3),'InputRange',[0 5]);
    %addchannel(ai, handles.rpm_ai_ch);
    %set(handles.ai.Channel(4),'InputRange',[0 10]);

    set(handles.ai,'SampleRate',10);
    set(handles.ai,'SamplesPerTrigger',10*sum(handles.period_time.*handles.period_use));

    % Start stopwatch
    tstart = tic;

    % Start stiring
    V = 10*(handles.rpm_val - handles.rpm_min)/(handles.rpm_max - handles.rpm_min);
    putsample(handles.ao, [V 0]);

    % Set textcolor to red
    set(handles.text11, 'ForegroundColor', 'r');
    set(handles.text12, 'ForegroundColor', 'r');
    set(handles.text13, 'ForegroundColor', 'r');
    set(handles.text14, 'ForegroundColor', 'r');
    set(handles.text15, 'ForegroundColor', 'r');
    set(handles.text16, 'ForegroundColor', 'r');
    set(handles.text10, 'String', handles.period_name(1));
    drawnow

    % Start logging
    start(handles.ai);

    % Graph properties
    set(handles.axes1, 'NextPlot', 'add');
    set(handles.axes2, 'NextPlot', 'add');
    set(handles.axes3, 'NextPlot', 'add');
    set(handles.axes4, 'NextPlot', 'add');

    set(gcf, 'DoubleBuffer', 'on'); % Prevent screen flashing

    % Conditioning
    t = NaN(4);
    t2 = zeros(3);
    played = 0;
    while floor(toc(tstart)) < handles.period_time(1)*handles.period_use(1)
        % Beep sounds to indicate period ends
        if floor(toc(tstart)) == handles.period_time(1)*handles.period_use(1) - 2 && played == 0
           y = wavread('start.wav');
           sound(y, 22050);
           played = 1;
        elseif floor(toc(tstart)) == handles.period_time(1)*handles.period_use(1) - 1 && played == 1
           y = wavread('start.wav');
           sound(y, 22050);
           played = 2;
        elseif floor(toc(tstart)) == handles.period_time(1)*handles.period_use(1) && played == 2
           y = wavread('notify.wav');
           sound(y, 22050);
        end
        % Update overall and remaining period time
        t(1) = handles.period_time(1) - floor(toc(tstart));
        t(2) = floor(t(1)/60);
        t(1) = t(1) - 60*t(2);
        t(3) = floor(t(1)/10);
        t(4) = t(1) - 10*t(3);
        if t2(1:3) == t(2:4)
        elseif t2 == zeros(3)
        else
            if mod(t(4), 2) == 1
                value = ['-' num2str(t(2)) ':' num2str(t(3)) num2str(t(4))];
            else
                value = ['-' num2str(t(2)) ' ' num2str(t(3)) num2str(t(4))];
            end
            set(handles.text11, 'String', value);
            set(handles.text13, 'String', value);

            if toc(tstart) > 1
                % Update read values
                data = []; %#ok<*NASGU>
                out = daqfind;
                if isempty(out) == 1
                   return 
                end
                data = peekdata(handles.ai, 10);
                data(1,1:3) = sum(data)/length(data);

                if data(1,1) > 0.98*4*handles.pH_ohm/1000;
                    data(1,1) = handles.pH_min + (data(1,1)-(4*handles.pH_ohm/1000))*(handles.pH_max - handles.pH_min)/((20*handles.pH_ohm/1000)-(4*handles.pH_ohm/1000));
                else
                    data(1,1) = NaN;
                end

                if data(1,2) > 0.98*4*handles.lpm_ohm/1000;
                    data(1,2) = handles.lpm_min + (data(1,2)-(4*handles.lpm_ohm/1000))*(handles.lpm_max - handles.lpm_min)/((20*handles.lpm_ohm/1000)-(4*handles.lpm_ohm/1000));
                else
                    data(1,2) = NaN;
                end

                if data(1,3) > 0.98*4*handles.temp_ohm/1000;
                    data(1,3) = handles.temp_min + (data(1,3)-(4*handles.temp_ohm/1000))*(handles.temp_max - handles.temp_min)/((20*handles.temp_ohm/1000)-(4*handles.temp_ohm/1000));
                else
                    data(1,3) = NaN;
                end

                % When RPM is set measurements are not necessary
                %data(1,4) = 10*(handles.rpm_val - handles.rpm_min)/(handles.rpm_max - handles.rpm_min);
                %if data(1,4) > 1.95
                %    data(1,4) = handles.rpm_min + data(1,4)*(handles.rpm_max - handles.rpm_min)/10;
                %else
                %    data(1,4) = NaN;
                %end

                % Add scatter to graph
                scatter(handles.axes1, -1*handles.period_time(1) + floor(toc(tstart)) - 0.5, data(1,1), 'go');
                scatter(handles.axes2, -1*handles.period_time(1) + floor(toc(tstart)) - 0.5, data(1,2), 'go');
                scatter(handles.axes3, -1*handles.period_time(1) + floor(toc(tstart)) - 0.5, data(1,3), 'go');
                scatter(handles.axes4, -1*handles.period_time(1) + floor(toc(tstart)) - 0.5, round(handles.rpm_val), 'go');

                value = [];
                value = num2str(round(data(1,1)*100));
                if and(length(value) == 4, strcmp(value, 'NaN') == 0) == 1;
                    value = [value(1:2) '.' value(3:4)];
                elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1;
                    value = ['0' value(1) '.' value(2:3)];
                elseif strcmp(value, 'NaN') == 1
                    value = '--.--';
                end
                set(handles.text12, 'String', value);

                value = [];
                value = round(data(1,2)*100);
                if value > 0
                    value = NaN;
                end

                value = num2str(value);
                if value(1) == '-';
                    value = '00.00';
                elseif and(length(value) == 4, strcmp(value, 'NaN') == 0) == 1;
                    value = [value(1:2) '.' value(3:4)];
                elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1;
                    value = ['0' value(1) '.' value(2:3)];
                elseif and(length(value) == 2, strcmp(value, 'NaN') == 0) == 1;
                    if str2double(value) > 25
                        value = ['00.' value(1:2)];
                    else
                        value = '00.00';
                    end
                else
                    value = '00.00';
                end
                set(handles.text14, 'String', value);

                value = [];
                value = num2str(round(data(1,3)*100));
                if and(length(value) == 4, strcmp(value, 'NaN') == 0) == 1;
                    value = [value(1:2) '.' value(3:4)];
                elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1;
                    value = ['0' value(1) '.' value(2:3)];
                elseif strcmp(value, 'NaN') == 1
                    value = '--.--';
                end
                set(handles.text15, 'String', value);

                value = [];
                value = num2str(round(handles.rpm_val));
                if length(value) == 4
                elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1
                    value = ['0' value(1:3)];
                elseif length(value) == 2
                    value = ['00' value(1:2)];
                elseif length(value) == 1
                    value = ['000' value(1)];
                elseif strcmp(value, 'NaN') == 1
                    value = '----';
                end
                set(handles.text16, 'String', value);
           end
                drawnow
        end
        t2(1:3)=t(2:4);
        % Read values of other variables
    end

    % Start air flow

    %V2 = 5*(handles.lpm_val - handles.lpm_min)/(handles.lpm_max - handles.lpm_min);
    V2 = 5*(handles.lpm_val - 0)/(10 - 0);
    putsample(handles.ao, [V V2]);

    % Flotation (periods)
    for I=2:11
        if handles.period_use(I) == 1
            set(handles.text10, 'String', handles.period_name(I));
            drawnow
        end
        t = NaN(5);
        t2 = [9 9 9 9];
        t3 = zeros(4);
        played = 0;
        while floor(toc(tstart)) <= sum(handles.period_time(1:I).*handles.period_use(1:I))
            % Beep sounds to indicate period ends
                if floor(toc(tstart)) == sum(handles.period_time(1:I).*handles.period_use(1:I)) - 2 && played == 0
                    y = wavread('start.wav');
                    sound(y, 22050);
                    played = 1;
                elseif floor(toc(tstart)) == sum(handles.period_time(1:I).*handles.period_use(1:I)) - 1 && played == 1
                    y = wavread('start.wav');
                    sound(y, 22050);
                    played = 2;
                elseif floor(toc(tstart)) == sum(handles.period_time(1:I).*handles.period_use(1:I)) && played == 2
                    y = wavread('notify.wav');
                    sound(y, 22050);
                    played = 3;
                end
            % Update overall and remaining period time
            t(1) = floor(toc(tstart)) - sum(handles.period_time(1).*handles.period_use(1));
            t(2) = floor(t(1)/600);
            t(1) = t(1) - 600*t(2);
            t(3) = floor(t(1)/60);
            t(1) = t(1) - 60*t(3); 
            t(4) = floor(t(1)/10);
            t(5) = t(1) - 10*t(4);

            t3(1) = sum(handles.period_time(1:I).*handles.period_use(1:I)) - floor(toc(tstart));
            t3(2) = floor(t3(1)/60);
            t3(1) = t3(1) - 60*t3(2);
            t3(3) = floor(t3(1)/10);
            t3(4) = t3(1) - 10*t3(3);

            if t2(1:4) == t(2:5)
            else
                if mod(t(5), 2) == 1
                    value = [num2str(t(2)) num2str(t(3)) ' ' num2str(t(4)) num2str(t(5))];
                    value2 = ['-' num2str(t3(2)) ' ' num2str(t3(3)) num2str(t3(4))];
                else
                    value = [num2str(t(2)) num2str(t(3)) ':' num2str(t(4)) num2str(t(5))];
                    value2 = ['-' num2str(t3(2)) ':' num2str(t3(3)) num2str(t3(4))];
                end
                set(handles.text11, 'String', value);
                set(handles.text13, 'String', value2);

                % Update read values
                data = [];
                out = daqfind;
                if isempty(out) == 1
                   return 
                end
                data = peekdata(handles.ai, 10);
                
                % Calculate mean values of lpm, temp and rpm
                stdev_pH = std(data(1:10,1));
                mean_pH = mean(data(1:10,1));
                % Remove outsider values
                for J = 1:10
                    if data(J,1) > mean_pH + 1.15*stdev_pH
                        data(J,1) = 0;
                    elseif data(J,1) < mean_pH - 1.15*stdev_pH
                        data(J,1) = 0;
                    end
                end
                data(1,1) = sum(data(1:10,1))/(10-sum(data(1:10,1)==0));

                data(1,2:3) = sum(data(1:10,2:3))/length(data(1:10,2:3));

                if data(1,1) > 0.98*4*handles.pH_ohm/1000;
                    data(1,1) = handles.pH_min + (data(1,1)-(4*handles.pH_ohm/1000))*(handles.pH_max - handles.pH_min)/((20*handles.pH_ohm/1000)-(4*handles.pH_ohm/1000));
                else
                    data(1,1) = NaN;
                end

                if data(1,2) > 0.98*4*handles.lpm_ohm/1000
                    data(1,2) = handles.lpm_min + (data(1,2)-(4*handles.lpm_ohm/1000))*(handles.lpm_max - handles.lpm_min)/((20*handles.lpm_ohm/1000)-(4*handles.lpm_ohm/1000));
                else
                    data(1,2) = NaN;
                end

                if data(1,3) > 0.98*4*handles.temp_ohm/1000;
                    data(1,3) = handles.temp_min + (data(1,3)-(4*handles.temp_ohm/1000))*(handles.temp_max - handles.temp_min)/((20*handles.temp_ohm/1000)-(4*handles.temp_ohm/1000));
                else
                    data(1,3) = NaN;
                end

                % When RPM is set measurements are not necessary
                %data(1,4) = 10*(handles.rpm_val - handles.rpm_min)/(handles.rpm_max - handles.rpm_min);
                %if data(1,4) > 1.95
                %    data(1,4) = handles.rpm_min + data(1,4)*(handles.rpm_max - handles.rpm_min)/10;
                %else
                %    data(1,4) = NaN;
                %end
                
                % Add scatter to graph
                scatter(handles.axes1, -1*(handles.period_time(1)*handles.period_use(1) - floor(toc(tstart))) - 0.5, data(1,1), 'go');
                scatter(handles.axes2, -1*(handles.period_time(1)*handles.period_use(1) - floor(toc(tstart))) - 0.5, data(1,2), 'go');
                scatter(handles.axes3, -1*(handles.period_time(1)*handles.period_use(1) - floor(toc(tstart))) - 0.5, data(1,3), 'go');
                scatter(handles.axes4, -1*(handles.period_time(1)*handles.period_use(1) - floor(toc(tstart))) - 0.5, round(handles.rpm_val), 'go');

                value = [];
                value = num2str(round(data(1,1)*100));
                if and(length(value) == 4, strcmp(value, 'NaN') == 0) == 1;
                    value = [value(1:2) '.' value(3:4)];
                elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1;
                    value = ['0' value(1) '.' value(2:3)];
                elseif strcmp(value, 'NaN') == 1
                    value = '--.--';
                end
                set(handles.text12, 'String', value);

                value = [];
                value = num2str(round(data(1,2)*100));
                if value(1) == '-';
                    value = '00.00';
                elseif and(length(value) == 4, strcmp(value, 'NaN') == 0) == 1;
                    value = [value(1:2) '.' value(3:4)];
                elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1;
                    value = ['0' value(1) '.' value(2:3)];
                elseif and(length(value) == 2, strcmp(value, 'NaN') == 0) == 1;
                    if str2double(value) > 25
                        value = ['00.' value(1:2)];
                    else
                        value = '00.00';
                    end
                else
                    value = '00.00';
                end
                set(handles.text14, 'String', value);

                value = [];
                value = num2str(round(data(1,3)*100));
                if and(length(value) == 4, strcmp(value, 'NaN') == 0) == 1;
                    value = [value(1:2) '.' value(3:4)];
                elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1;
                    value = ['0' value(1) '.' value(2:3)];
                elseif strcmp(value, 'NaN') == 1
                    value = '--.--';
                end
                set(handles.text15, 'String', value);

                value = [];
                value = num2str(round(handles.rpm_val));
                if length(value) == 4
                elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1
                    value = ['0' value(1:3)];
                elseif length(value) == 2
                    value = ['00' value(1:2)];
                elseif length(value) == 1
                    value = ['000' value(1)];
                elseif strcmp(value, 'NaN') == 1
                    value = '----';
                end
                set(handles.text16, 'String', value);

                drawnow
            end
            t2(1:4)=t(2:5);
        end
    end


    % Stop stiring and air flow
    putsample(handles.ao, [0 0]);

    % Change text color to black

    set(handles.text10, 'String', 'Off');
    set(handles.text11, 'ForegroundColor', 'k');
    set(handles.text12, 'ForegroundColor', 'k');
    set(handles.text13, 'ForegroundColor', 'k');
    set(handles.text14, 'ForegroundColor', 'k');
    set(handles.text15, 'ForegroundColor', 'k');
    set(handles.text16, 'ForegroundColor', 'k');

    % Calculate value for each second of flotation
    handles.data = [];

    wait(handles.ai, 2);
    data_s = [];
    handles.data = getdata(handles.ai);
    t = handles.period_use(1)*handles.period_time(1);
    t2 = sum(handles.period_use.*handles.period_time);
    for I = 1 : (t2 - t)
        sta = 1 + (t + I - 1)*10;
        sto = (t + I)*10;

        if sum(handles.data(sta : sto, 1))/10 > 0.98*4*handles.pH_ohm/1000 == 1
            data_s(I, 1) = sum(handles.data(sta : sto, 1))/10;
        else
            data_s(I, 1) = NaN;
        end

        if sum(handles.data(sta : sto, 2))/10 > 0.98*4*handles.lpm_ohm/1000 == 1
            data_s(I, 2) = sum(handles.data(sta : sto, 2))/10;
        else
            data_s(I, 2) = NaN;
        end

        if sum(handles.data(sta : sto, 3))/10 > 0.98*4*handles.temp_ohm/1000 == 1
            data_s(I, 3) = sum(handles.data(sta : sto, 3))/10;
        else
            data_s(I, 3) = NaN;
        end

        %if sum(handles.data(sta : sto, 4))/10 > 1.95
        %    data_s(I, 4) = sum(handles.data(sta : sto, 4))/10;
        %else
        %    data_s(I, 4) = NaN; 
        %end
    end

    % figure(1);
    % plot(data_s);

    % Calculate and set average values to show as last
    data_s(:,1) = handles.pH_min + (data_s(:,1) - (4*handles.pH_ohm/1000)).*((handles.pH_max - handles.pH_min)/((20*handles.pH_ohm/1000)-(4*handles.pH_ohm/1000)));
    data_s(:,2) = handles.lpm_min + (data_s(:,2) - (4*handles.lpm_ohm/1000)).*((handles.lpm_max - handles.lpm_min)/((20*handles.lpm_ohm/1000)-(4*handles.lpm_ohm/1000)));
    data_s(:,3) = handles.temp_min + (data_s(:,3) - (4*handles.temp_ohm/1000)).*((handles.temp_max - handles.temp_min)/((20*handles.temp_ohm/1000)-(4*handles.temp_ohm/1000)));
    %data_s(:,4) = handles.rpm_min + data_s(:,4).*((handles.rpm_max - handles.rpm_min)/10);

    value = [];
    value = num2str(round(nansum(data_s(:,1))/(length(data_s(:,1)) - sum(isnan(data_s(:,1))))*100));
    if and(length(value) == 4, strcmp(value, 'NaN') == 0) == 1
           value = [value(1:2) '.' value(3:4)];
    elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1
           value = ['0' value(1) '.' value(2:3)];
    elseif strcmp(value, 'NaN') == 1
           value = '--.--';
    end
    set(handles.text12, 'String', value);

    value = [];
    value = num2str(round(nansum(data_s(:,2))/(length(data_s(:,2)) - sum(isnan(data_s(:,2))))*100));
    if and(length(value) == 4, strcmp(value, 'NaN') == 0) == 1
           value = [value(1:2) '.' value(3:4)];
    elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1
           value = ['0' value(1) '.' value(2:3)];
    elseif and(length(value) == 2, strcmp(value, 'NaN') == 0) == 1       
           if str2double(value) > 25
                 value = ['00.' value(1:2)];
           else
                 value = '--.--';
           end
    elseif strcmp(value, 'NaN') == 1
           value = '--.--';
    end
    set(handles.text14, 'String', value);

    value = [];
    value = num2str(round(nansum(data_s(:,3))/(length(data_s(:,3)) - sum(isnan(data_s(:,3))))*100));
    if and(length(value) == 4, strcmp(value, 'NaN') == 0) == 1
           value = [value(1:2) '.' value(3:4)];
    elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1
           value = ['0' value(1) '.' value(2:3)];
    elseif strcmp(value, 'NaN') == 1
           value = '--.--';
    end
    set(handles.text15, 'String', value);

    value = [];
    %value = num2str(round(nansum(data_s(:,4))/(length(data_s(:,4)) - sum(isnan(data_s(:,4))))));
    value = num2str(handles.rpm_val);
    if length(value) == 4
    elseif and(length(value) == 3, strcmp(value, 'NaN') == 0) == 1
           value = ['0' value(1:3)];
    elseif length(value) == 2
           value = ['00' value(1:2)];
    elseif length(value) == 1
           value = ['000' value(1)];
    elseif strcmp(value, 'NaN') == 1
           value = '----';
    end
    set(handles.text16, 'String', value);

    t = [];

    t(1) = sum(handles.period_time(2:11).*handles.period_use(2:11));
    t(2) = floor(t(1)/600);
    t(1) = t(1) - 600*t(2);
    t(3) = floor(t(1)/60);
    t(1) = t(1) - 60*t(3); 
    t(4) = floor(t(1)/10);
    t(5) = t(1) - 10*t(4);

    set(handles.text11, 'String', [num2str(t(2)), num2str(t(3)), ':', num2str(t(4)), num2str(t(5))]);
    set(handles.text13, 'String', '--:--');
    drawnow
    
    % Delete channels
    delete(handles.ao);
    clear handles.ao;
    delete(handles.ai);
    clear handles.ai;
    set(hObject, 'String', 'START');
else % Turn off
    set(hObject, 'String', 'START');
    
    % Change text color to black
    set(handles.text10, 'String', 'Off');
    set(handles.text11, 'ForegroundColor', 'k');
    set(handles.text12, 'ForegroundColor', 'k');
    set(handles.text13, 'ForegroundColor', 'k');
    set(handles.text14, 'ForegroundColor', 'k');
    set(handles.text15, 'ForegroundColor', 'k');
    set(handles.text16, 'ForegroundColor', 'k');
    
    % Delete channels
    out = daqfind;
    putsample(out(1), [0 0]);
    stop(daqfind);
    delete(daqfind);
    daqreset;
    return;
end
    
guidata(hObject, handles)


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

clear = questdlg('Do you want to clear all data?', 'Confirmation', 'No');

if strcmp(clear, 'Yes') == 1
    t(1) = handles.period_time(1)*handles.period_use(1);
    t(2) = floor(t(1)/60);
    t(1) = round(t(1) - 60*t(2));
    t(3) = floor(t(1)/10);
    t(4) = t(1) - 10*t(3);
    value = ['-' num2str(t(2)) ':' num2str(t(3)) num2str(t(4))];
    set(handles.text11, 'String', value);
    set(handles.text13, 'String', value);
    set(handles.text12, 'String', '07.00');
    set(handles.text14, 'String', '00.00');
    set(handles.text15, 'String', '20.00');
    set(handles.text16, 'String', '0000');

    cla(handles.axes1);
    cla(handles.axes2);
    cla(handles.axes3);
    cla(handles.axes4);

    min = -1*handles.period_time(1)*handles.period_use(1);
    max = sum(handles.period_time(2:11).*handles.period_use(2:11));
    set(handles.axes1, 'XLim', [min max]);
    set(handles.axes2, 'XLim', [min max]);
    set(handles.axes3, 'XLim', [min max]);
    set(handles.axes4, 'XLim', [min max]);

    refresh_axes(handles);
    
    drawnow
    
    handles.data = [];
end
guidata(hObject, handles);

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

% Calculate value for each second of flotation
if isempty(handles.data) == 1
    uiwait(errordlg('Nothing to save', 'Error', 'modal'));
else
    t = handles.period_use(1)*handles.period_time(1);
    t2 = sum(handles.period_use.*handles.period_time);
    for I = 1 : t2
        sta = 1 + (I - 1)*10;
        sto = (I)*10;
        
        % Calculate mean values of lpm, temp and rpm
        stdev_pH = std(handles.data(sta : sto, 1));
        mean_pH = mean(handles.data(sta : sto,1));
        % Remove outsider values of pH voltage
        for J = 1:10
            if handles.data(sta + J-1,1) > mean_pH + 1.15*stdev_pH
                handles.data(sta + J-1,1) = 0;
            elseif handles.data(sta +J-1,1) < mean_pH - 1.15*stdev_pH
                handles.data(sta + J-1,1) = 0;
            end
        end
        
        if sum(handles.data(sta : sto, 1))/(10-sum(handles.data(sta : sto,1)==0)) > 0.98*4*handles.pH_ohm/1000
            data_s(I+6, 2) = sum(handles.data(sta : sto, 1))/(10-sum(handles.data(sta : sto,1)==0));
        else
            data_s(I+6, 2) = NaN;
        end
        
        if sum(handles.data(sta : sto, 2))/10 > 1.1*4*handles.lpm_ohm/1000
            data_s(I+6, 3) = sum(handles.data(sta : sto, 2))/10;
        else
            data_s(I+6, 3) = NaN;
        end
        
        if sum(handles.data(sta : sto, 3))/10 > 0.98*4*handles.temp_ohm/1000
            data_s(I+6, 4) = sum(handles.data(sta : sto, 3))/10;
        else
            data_s(I+6, 4) = NaN;
        end
        
        data_s(I+6, 5) = handles.rpm_val;
        
        data_s(I+6, 1) = -1*t + (I - 0.5);
    end
    
    data_s(7:length(data_s),2) = round((handles.pH_min + (data_s(7:length(data_s),2)-(4*handles.pH_ohm/1000))*(handles.pH_max - handles.pH_min)/((20*handles.pH_ohm/1000)-(4*handles.pH_ohm/1000))).*100)./100;
    data_s(7:length(data_s),3) = round((handles.lpm_min + (data_s(7:length(data_s),3)-(4*handles.lpm_ohm/1000))*(handles.lpm_max - handles.lpm_min)/((20*handles.lpm_ohm/1000)-(4*handles.lpm_ohm/1000))).*100)./100;
    % Ensure there are no negative values of air lpm (while conditioning)
    data_s(7:7+handles.period_use(1)*handles.period_time(1),3) = 0;
    data_s(7:length(data_s),4) = round((handles.temp_min + (data_s(7:length(data_s),4)-(4*handles.temp_ohm/1000))*(handles.temp_max - handles.temp_min)/((20*handles.temp_ohm/1000)-(4*handles.temp_ohm/1000))).*100)./100;
    %data_s(7:length(data_s),5) = round(handles.rpm_min + data_s(7:length(data_s),5)*(handles.rpm_max - handles.rpm_min)/10);
    data_s(7:length(data_s),5) = handles.rpm_val;
    
    string = get(handles.text13, 'String');
    handles.data_s = data_s;
    uiwait(sample_name(handles));
    handles.answer = get(handles.text13, 'String');
    set(handles.text13, 'String', string);
    save_xls(handles)
end

guidata(hObject, handles);



% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, ~, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.set = 1;
subgui_handle = set_mm;
subgui_data_handles = guidata(subgui_handle);
subgui_data_handles.set = handles.set;
subgui_data_handles.pH_min_graph = handles.pH_min_graph;
subgui_data_handles.pH_max_graph = handles.pH_max_graph;
set(subgui_data_handles.edit1, 'String', num2str(handles.pH_max_graph, '%2.2f'));
set(subgui_data_handles.edit2, 'String', num2str(handles.pH_min_graph, '%2.2f'));
guidata(subgui_handle, subgui_data_handles);
guidata(hObject, handles)


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, ~, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.set = 4;
subgui_handle = set_mms;
subgui_data_handles = guidata(subgui_handle);
subgui_data_handles.set = handles.set;
subgui_data_handles.rpm_min_graph = handles.rpm_min_graph;
subgui_data_handles.rpm_max_graph = handles.rpm_max_graph;
subgui_data_handles.rpm_val = handles.rpm_val;
set(subgui_data_handles.edit1, 'String', num2str(handles.rpm_max_graph, '%4.0f'));
set(subgui_data_handles.edit2, 'String', num2str(handles.rpm_min_graph, '%4.0f'));
set(subgui_data_handles.edit3, 'String', num2str(handles.rpm_val, '%4.0f'));
guidata(subgui_handle, subgui_data_handles);
guidata(hObject, handles)

% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, ~, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.set = 2;
subgui_handle = set_mm;
subgui_data_handles = guidata(subgui_handle);
subgui_data_handles.set = handles.set;
subgui_data_handles.temp_min_graph = handles.temp_min_graph;
subgui_data_handles.temp_max_graph = handles.temp_max_graph;
set(subgui_data_handles.edit1, 'String', num2str(handles.temp_max_graph, '%2.2f'));
set(subgui_data_handles.edit2, 'String', num2str(handles.temp_min_graph, '%2.2f'));
guidata(subgui_handle, subgui_data_handles);
guidata(hObject, handles)

% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, ~, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.set = 3;
subgui_handle = set_mms;
subgui_data_handles = guidata(subgui_handle);
subgui_data_handles.set = handles.set;
subgui_data_handles.lpm_min_graph = handles.lpm_min_graph;
subgui_data_handles.lpm_max_graph = handles.lpm_max_graph;
subgui_data_handles.lpm_val = handles.lpm_val;
set(subgui_data_handles.edit1, 'String', num2str(handles.lpm_max_graph, '%2.2f'));
set(subgui_data_handles.edit2, 'String', num2str(handles.lpm_min_graph, '%2.2f'));
set(subgui_data_handles.edit3, 'String', num2str(handles.lpm_val, '%2.2f'));
guidata(subgui_handle, subgui_data_handles);
guidata(hObject, handles)


% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, ~, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
subgui_handle = period;
subgui_data_handles = guidata(subgui_handle);
subgui_data_handles.period_name = handles.period_name;
subgui_data_handles.period_time = handles.period_time;
subgui_data_handles.period_use = handles.period_use;
data = cell(11, 5);
data(1:11, 2) = num2cell(handles.period_time);
data(1:11, 5) = handles.period_name;
for I = 1:11
   if handles.period_use(I) == 1
       data(I, 1) = {'Yes'};
   else
       data(I, 1) = {'No'};
   end
end
data(1, 3) = num2cell(-1*handles.period_use(1)*handles.period_time(1));
data(1, 4) = num2cell(0);
for I = 1:10
   data(I+1, 4) = num2cell(sum(handles.period_use(2:I+1).*handles.period_time(2:I+1)));
   data(I+1, 3) = num2cell(sum(handles.period_use(2:I).*handles.period_time(2:I)));
end

set(subgui_data_handles.uitable1, 'Data', data)
guidata(subgui_handle, subgui_data_handles);
guidata(hObject, handles)

% --- Executes on key press with focus on pushbutton5 and none of its controls.
function pushbutton5_KeyPressFcn(hObject, ~, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)
guidata(hObject, handles);
    
% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(~, ~, ~)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

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

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


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


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


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

function refresh_axes(handles)
min = handles.pH_min_graph;
max = handles.pH_max_graph;
set(handles.axes1, 'YLim', [min max]);

min = handles.lpm_min_graph;
max = handles.lpm_max_graph;
set(handles.axes2, 'YLim', [min max]);

min = handles.temp_min_graph;
max = handles.temp_max_graph;
set(handles.axes3, 'YLim', [min max]);

min = handles.rpm_min_graph;
max = handles.rpm_max_graph;
set(handles.axes4, 'YLim', [min max]);

min = -1*handles.period_time(1)*handles.period_use(1);
max = sum(handles.period_time(2:11).*handles.period_use(2:11));
set(handles.axes1, 'XLim', [min max]);
set(handles.axes2, 'XLim', [min max]);
set(handles.axes3, 'XLim', [min max]);
set(handles.axes4, 'XLim', [min max]);

t(1) = handles.period_time(1)*handles.period_use(1);
t(2) = floor(t(1)/60);
t(1) = round(t(1) - 60*t(2));
t(3) = floor(t(1)/10);
t(4) = t(1) - 10*t(3);
value = ['-' num2str(t(2)) ':' num2str(t(3)) num2str(t(4))];
set(handles.text11, 'String', value);
set(handles.text13, 'String', value);


% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, ~, handles)
% hObject    handle to Untitled_1 (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);

% --------------------------------------------------------------------
function Untitled_4_Callback(hObject, ~, handles)
% hObject    handle to Untitled_4 (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);

% --------------------------------------------------------------------
function Untitled_8_Callback(hObject, ~, handles)
% hObject    handle to Untitled_8 (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);

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

subgui_handle = config;
subgui_data_handles = guidata(subgui_handle);
subgui_data_handles.pH_min = handles.pH_min;
subgui_data_handles.pH_max = handles.pH_max;
subgui_data_handles.pH_ohm = handles.pH_ohm;
subgui_data_handles.lpm_min = handles.lpm_min;
subgui_data_handles.lpm_max = handles.lpm_max;
subgui_data_handles.lpm_ohm = handles.lpm_ohm;
subgui_data_handles.temp_min = handles.temp_min;
subgui_data_handles.temp_max = handles.temp_max;
subgui_data_handles.temp_ohm = handles.temp_ohm;
subgui_data_handles.rpm_min = handles.rpm_min;
subgui_data_handles.rpm_max = handles.rpm_max;
subgui_data_handles.default_save_path = handles.default_save_path;

set(subgui_data_handles.edit1, 'String', num2str(handles.pH_min, '%2.4f'));
set(subgui_data_handles.edit2, 'String', num2str(handles.pH_max, '%2.4f'));
set(subgui_data_handles.edit3, 'String', num2str(handles.lpm_min, '%2.4f'));
set(subgui_data_handles.edit4, 'String', num2str(handles.lpm_max, '%2.4f'));
set(subgui_data_handles.edit5, 'String', num2str(handles.temp_min, '%2.4f'));
set(subgui_data_handles.edit6, 'String', num2str(handles.temp_max, '%2.4f'));
set(subgui_data_handles.edit8, 'String', num2str(handles.rpm_min, '%4.2f'));
set(subgui_data_handles.edit9, 'String', num2str(handles.rpm_max, '%4.2f'));
set(subgui_data_handles.edit10, 'String', num2str(handles.pH_ohm, '%3.3f'));
set(subgui_data_handles.edit11, 'String', num2str(handles.lpm_ohm, '%3.3f'));
set(subgui_data_handles.edit12, 'String', num2str(handles.temp_ohm, '%3.3f'));

set(subgui_data_handles.edit7, 'String', handles.default_save_path);
guidata(subgui_handle, subgui_data_handles);

guidata(hObject, handles);

% --------------------------------------------------------------------
function Untitled_10_Callback(hObject, ~, handles) %Save conf
% 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)

%saveData = cell(17, 2);
saveData = cell(9, 2);

%saveData(10,1) = {'ai_card:'};
%saveData(10,2) = num2cell(handles.ai_card);
%saveData(11,1) = {'ao_card:'};
%saveData(11,2) = num2cell(handles.ao_card);
%saveData(12,1) = {'rpm_ao_ch:'};
%saveData(12,2) = num2cell(handles.rpm_ao_ch);
%saveData(13,1) = {'lpm_ao_ch:'};
%saveData(13,2) = num2cell(handles.lpm_ao_ch);
%saveData(14,1) = {'pH_ai_ch:'};
%saveData(14,2) = num2cell(handles.pH_ai_ch);
%saveData(15,1) = {'lpm_ai_ch:'};
%saveData(15,2) = num2cell(handles.lpm_ai_ch);
%saveData(16,1) = {'temp_ai_ch:'};
%saveData(16,2) = num2cell(handles.temp_ai_ch);
%saveData(17,1) = {'rpm_ai_ch:'};
%saveData(17,2) = num2cell(handles.rpm_ai_ch);

saveData(1,1) = {'pH_min:'};
saveData(1,2) = num2cell(handles.pH_min);
saveData(2,1) = {'pH_max:'};
saveData(2,2) = num2cell(handles.pH_max);
saveData(3,1) = {'lpm_min:'};
saveData(3,2) = num2cell(handles.lpm_min);
saveData(4,1) = {'lpm_max:'};
saveData(4,2) = num2cell(handles.lpm_max);
saveData(5,1) = {'temp_min:'};
saveData(5,2) = num2cell(handles.temp_min);
saveData(6,1) = {'temp_max:'};
saveData(6,2) = num2cell(handles.temp_max);
saveData(7,1) = {'rpm_min:'};
saveData(7,2) = num2cell(handles.rpm_min);
saveData(8,1) = {'rpm_max:'};
saveData(8,2) = num2cell(handles.rpm_max);
saveData(9,1) = {'pH_ohm:'};
saveData(9,2) = num2cell(handles.pH_ohm);
saveData(10,1) = {'lpm_ohm:'};
saveData(10,2) = num2cell(handles.lpm_ohm);
saveData(11,1) = {'temp_ohm:'};
saveData(11,2) = num2cell(handles.temp_ohm);
saveData(12,1) = {handles.default_save_path};
saveData(12,2) = num2cell(1);

path = pwd();
cd(handles.default_save_path)
[filename, pathname] = uiputfile('*.conf', 'Save configuration to file');    
cd(path);
if isnumeric(filename) == 1 || isnumeric(pathname) == 1 
else
   dlmcell([pathname filename], saveData, ' ');
end

guidata(hObject, handles);

% --------------------------------------------------------------------
function Untitled_11_Callback(hObject, ~, handles) %Load conf
% 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)
path = pwd();
cd(handles.default_save_path)
[filename, pathname] = uigetfile('*.conf', 'Load configuration from file');    
cd(path);
if isnumeric(filename) == 1 || isnumeric(pathname) == 1 
else
   [names, values] = textread([pathname filename], '%s%n');
end

handles.pH_min = values(1);
handles.pH_max = values(2);
handles.lpm_min = values(3);
handles.lpm_max = values(4);
handles.temp_min = values(5);
handles.temp_max = values(6);
handles.rpm_min = values(7);
handles.rpm_max = values(8);
handles.pH_ohm = values(9);
handles.lpm_ohm = values(10);
handles.temp_ohm = values(11);

handles.default_save_path = names{12};

guidata(hObject, handles);


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

saveData = cell(21, 3);

saveData(1,1) = {'Set_air_flow:'};
saveData(1,2) = num2cell(handles.lpm_val);
saveData(2,1) = {'Min_air_flow_on_graph:'};
saveData(2,2) = num2cell(handles.lpm_min_graph);
saveData(3,1) = {'Max_air_flow_on_graph:'};
saveData(3,2) = num2cell(handles.lpm_max_graph);
saveData(4,1) = {'Set_revolutions:'};
saveData(4,2) = num2cell(handles.rpm_val);
saveData(5,1) = {'Min_revolutions_on_graph:'};
saveData(5,2) = num2cell(handles.rpm_min_graph);
saveData(6,1) = {'Max_revolutions_on_graph:'};
saveData(6,2) = num2cell(handles.rpm_max_graph);
saveData(7,1) = {'Min_temperature_on_graph:'};
saveData(7,2) = num2cell(handles.temp_min_graph);
saveData(8,1) = {'Max_temperature_on_graph:'};
saveData(8,2) = num2cell(handles.temp_max_graph);
saveData(9,1) = {'Min_pH_on_graph:'};
saveData(9,2) = num2cell(handles.pH_min_graph);
saveData(10,1) = {'Max_pH_on_graph:'};
saveData(10,2) = num2cell(handles.pH_max_graph);

saveData(1:10,3) = num2cell(1);

for I=1:11
    period_name(I) = regexprep(handles.period_name(I), ' ', '_');
    saveData(10+I,1) = {period_name(I)};
    saveData(10+I,2) = {handles.period_time(I)};
    saveData(10+I,3) = {handles.period_use(I)};
end

path = pwd();
cd(handles.default_save_path)
[filename, pathname] = uiputfile('*.dat', 'Save preferences to file');    
cd(path);
if isnumeric(filename) == 1 || isnumeric(pathname) == 1 
else
   dlmcell([pathname filename], saveData, ' ');
end

guidata(hObject, handles);

% --------------------------------------------------------------------
function Untitled_6_Callback(hObject, ~, handles)
% hObject    handle to Untitled_6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
path = pwd();
cd(handles.default_save_path)
[filename, pathname] = uigetfile('*.dat', 'Load preferences from file');    
cd(path);
if isnumeric(filename) == 1 || isnumeric(pathname) == 1 
else
   [names, values, values2] = textread([pathname filename], '%s%n%n');
end

handles.lpm_val = values(1);
handles.lpm_min_graph = values(2);
handles.lpm_max_graph = values(3);
handles.rpm_val = values(4);
handles.rpm_min_graph = values(5);
handles.rpm_max_graph = values(6);
handles.temp_min_graph = values(7);
handles.temp_max_graph = values(8);
handles.pH_min_graph = values(9);
handles.pH_max_graph = values(10);

set(handles.axes1, 'YLim', [handles.pH_min_graph handles.pH_max_graph]);
set(handles.axes2, 'YLim', [handles.lpm_min_graph handles.lpm_max_graph]);
set(handles.axes3, 'YLim', [handles.temp_min_graph handles.temp_max_graph]);
set(handles.axes4, 'YLim', [handles.rpm_min_graph handles.rpm_max_graph]);

for I=1:11
    handles.period_name(I) = regexprep(names(10+I), '_', ' ');
    handles.period_time(I) = values(10+I);
    handles.period_use(I) = values2(10+I);
end

min = -1*handles.period_time(1)*handles.period_use(1);
max = sum(handles.period_time(2:11).*handles.period_use(2:11));
set(handles.axes1, 'XLim', [min max]);
set(handles.axes2, 'XLim', [min max]);
set(handles.axes3, 'XLim', [min max]);
set(handles.axes4, 'XLim', [min max]);

guidata(hObject, handles);

% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, ~, handles)
% hObject    handle to Untitled_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Calculate value for each second of flotation
if isempty(handles.data) == 1
    uiwait(errordlg('Nothing to save', 'Error', 'modal'));
else
    t = handles.period_use(1)*handles.period_time(1);
    t2 = sum(handles.period_use.*handles.period_time);
    for I = 1 : t2
        sta = 1 + (I - 1)*10;
        sto = (I)*10;
        if sum(handles.data(sta : sto, 1))/10 > 0.98*4*handles.pH_ohm/1000
            data_s(I+6, 2) = sum(handles.data(sta : sto, 1))/10;
        else
            data_s(I+6, 2) = NaN;
        end
        
        if sum(handles.data(sta : sto, 2))/10 > 0.98*4*handles.lpm_ohm/1000
            data_s(I+6, 3) = sum(handles.data(sta : sto, 2))/10;
        else
            data_s(I+6, 3) = NaN;
        end
        
        if sum(handles.data(sta : sto, 3))/10 > 0.98*4*handles.temp_ohm/1000
            data_s(I+6, 4) = sum(handles.data(sta : sto, 3))/10;
        else
            data_s(I+6, 4) = NaN;
        end
        
        data_s(I+6, 5) = sum(handles.data(sta : sto, 4))/10;
        
        data_s(I+6, 1) = -1*t + (I - 0.5);
    end
    
    data_s(7:length(data_s),2) = round((handles.pH_min + (data_s(7:length(data_s),2)-(4*handles.pH_ohm/1000))*(handles.pH_max - handles.pH_min)/((20*handles.pH_ohm/1000)-(4*handles.pH_ohm/1000))).*100)./100;
    data_s(7:length(data_s),3) = round((handles.lpm_min + (data_s(7:length(data_s),3)-(4*handles.lpm_ohm/1000))*(handles.lpm_max - handles.lpm_min)/((20*handles.lpm_ohm/1000)-(4*handles.lpm_ohm/1000))).*100)./100;
    data_s(7:length(data_s),4) = round((handles.temp_min + (data_s(7:length(data_s),4)-(4*handles.temp_ohm/1000))*(handles.temp_max - handles.temp_min)/((20*handles.temp_ohm/1000)-(4*handles.temp_ohm/1000))).*100)./100;
    data_s(7:length(data_s),5) = round(handles.rpm_val);
    
    string = get(handles.text13, 'String');
    handles.data_s = data_s;
    uiwait(sample_name(handles));
    handles.answer = get(handles.text13, 'String');
    set(handles.text13, 'String', string);
    save_xls(handles)
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function Untitled_3_Callback(hObject, ~, handles)
% hObject    handle to Untitled_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Calculate value for each second of flotation
if isempty(handles.data) == 1
    uiwait(errordlg('Nothing to save', 'Error', 'modal'));
else
    t = handles.period_use(1)*handles.period_time(1);
    t2 = sum(handles.period_use.*handles.period_time);
    for I = 1 : t2
        sta = 1 + (I - 1)*10;
        sto = (I)*10;
        if sum(handles.data(sta : sto, 1))/10 > 0.98*4*handles.pH_ohm/1000
            data_s(I+6, 2) = sum(handles.data(sta : sto, 1))/10;
        else
            data_s(I+6, 2) = NaN;
        end
        
        if sum(handles.data(sta : sto, 2))/10 > 0.98*4*handles.lpm_ohm/1000
            data_s(I+6, 3) = sum(handles.data(sta : sto, 2))/10;
        else
            data_s(I+6, 3) = NaN;
        end
        
        if sum(handles.data(sta : sto, 3))/10 > 0.98*4*handles.temp_ohm/1000
            data_s(I+6, 4) = sum(handles.data(sta : sto, 3))/10;
        else
            data_s(I+6, 4) = NaN;
        end
        
        %data_s(I+6, 5) = sum(handles.data(sta : sto, 4))/10;
        data_s(I+6, 5) = handles.rpm_val;
        
        data_s(I+6, 1) = -1*t + (I - 0.5);
    end
    
    data_s(7:length(data_s),2) = round((handles.pH_min + (data_s(7:length(data_s),2)-(4*handles.pH_ohm/1000))*(handles.pH_max - handles.pH_min)/((20*handles.pH_ohm/1000)-(4*handles.pH_ohm/1000))).*100)./100;
    data_s(7:length(data_s),3) = round((handles.lpm_min + (data_s(7:length(data_s),3)-(4*handles.lpm_ohm/1000))*(handles.lpm_max - handles.lpm_min)/((20*handles.lpm_ohm/1000)-(4*handles.lpm_ohm/1000))).*100)./100;
    data_s(7:length(data_s),4) = round((handles.temp_min + (data_s(7:length(data_s),4)-(4*handles.temp_ohm/1000))*(handles.temp_max - handles.temp_min)/((20*handles.temp_ohm/1000)-(4*handles.temp_ohm/1000))).*100)./100;
    data_s(7:length(data_s),5) = round(handles.rpm_val);
    
    string = get(handles.text13, 'String');
    handles.data_s = data_s;
    uiwait(sample_name(handles));
    handles.answer = get(handles.text13, 'String');
    set(handles.text13, 'String', string);
    save_txt(handles)
end
guidata(hObject, handles);

function save_xls(handles)

data_s = handles.data_s;
answer = handles.answer;

    data_s(7:length(data_s),5) = handles.rpm_val;

    if strcmp(answer, '--:--') == 1
        defaultname = datestr(now, 29);
        answer = '';
    elseif isnan(str2double(answer)) == 1    
        defaultname = strcat(datestr(now, 29), '_', answer);
    else
       if length(answer) == 1
           answer = strcat('00', answer);
       elseif length(answer) == 2
           answer = strcat('0', answer);
       end
       defaultname = strcat(datestr(now, 29), '_', answer);
    end
    
    data_s = num2cell(data_s);
    data_s(1,1:5) = [{'Sample description:'} {answer} {''} {''} {''}];
    data_s(2,1:5) = [{'Set airflow:'} {handles.lpm_val} {''} {''} {''}];
    data_s(3,1:5) = [{'stiring speed:'} {handles.rpm_val} {''} {''} {''}];
    data_s(4,1:5) = [{''} {''} {''} {''} {''}];
    data_s(5,1:5) = [{'Flotation time'} {'pH'} {'Air flow'} {'Temperature'} {'Stiring speed'}];
    data_s(6,1:5) = [{'[s]'} {'[ ]'} {'[lpm]'} {'[C]'} {'[rpm]'}];
    
    path = pwd();
    cd(handles.default_save_path);
    
    [filename, pathname] = uiputfile('*.xls', 'Save Excel 2003 file', defaultname);
    
    cd(path);
    
    if isnumeric(filename) == 1 || isnumeric(pathname) == 1 
    else
        [success, message] = xlswrite([pathname filename], data_s);
        if success == 0
            uiwait(errordlg(struct2cell(message), 'Error', 'modal'));
        end
    end    

function save_txt(handles)

data_s = handles.data_s;
answer = handles.answer;

    if strcmp(answer, '--:--') == 1
        defaultname = datestr(now, 29);
        answer = '';
    elseif isnan(str2double(answer)) == 1    
        defaultname = strcat(datestr(now, 29), '_', answer);
    else
       if length(answer) == 1
           answer = strcat('00', answer);
       elseif length(answer) == 2
           answer = strcat('0', answer);
       end
       defaultname = strcat(datestr(now, 29), '_', answer);
    end
    
    data_s = num2cell(data_s);
    data_s(1,1:5) = [{'Sample description:'} {answer} {''} {''} {''}];
    data_s(2,1:5) = [{'Set airflow:'} {handles.lpm_val} {''} {''} {''}];
    data_s(3,1:5) = [{'stiring speed:'} {handles.rpm_val} {''} {''} {''}];
    data_s(4,1:5) = [{''} {''} {''} {''} {''}];
    data_s(5,1:5) = [{'Flotation time'} {'pH'} {'Air flow'} {'Temperature'} {'Stiring speed'}];
    data_s(6,1:5) = [{'[s]'} {'[ ]'} {'[lpm]'} {'[C]'} {'[rpm]'}];
    
    path = pwd();
    cd(handles.default_save_path);
    
    [filename, pathname] = uiputfile('*.txt', 'Save text file', defaultname);
    
    cd(path);
    
    if isnumeric(filename) == 1 || isnumeric(pathname) == 1 
    else
        dlmcell([pathname filename], data_s, ';');
    end

Contact us