image thumbnail

Multivariate Data Analysis and Monitoring for the Process Industries



26 Feb 2008 (Updated )

Files from the webinar Multivariate Data Analysis and Monitoring for the Process Industries

function writeToDB

data = load('metal');

%% Create main figure

f = figure('units', 'normalized', 'position', [0.1,0.1,0.2,0.2],...
    'menubar', 'none', 'toolbar', 'none', 'nextplot', 'new',...
    'name','DB Write Utility','numbertitle','off','tag', 'writeToDBGUI');

%% Save data in figure's appdata
setappdata(f, 'data', data.metals);

%% Add start and stop buttons, and textbox
uicontrol('style', 'pushbutton', 'string', 'Start',...
    'units', 'normalized', 'position', [0.2,0.7,0.2,0.2],...
    'tag', 'writeToDBstartbut', 'callback', @writeToDBstartfcn);

uicontrol('style', 'pushbutton', 'string', 'Stop',...
    'units', 'normalized', 'position', [0.6,0.7,0.2,0.2],...
    'callback', @writeToDBstopfcn);

uicontrol('style', 'text',...
    'units', 'normalized', 'position', [0.2,0.2,0.6,0.4],...
    'tag', 'writeToDBtextbox');

%% Callbacks

%% Start button callback
    function writeToDBstartfcn(hObject,eventdata) %#ok
        % Get handle to main figure
        f = findobj('Tag', 'writeToDBGUI');
        % Make a connection to the database and clear the table
        s.DataReturnFormat = 'cellarray';
        s.ErrorHandling = 'store';
        s.NullNumberRead = 'NaN';
        s.NullNumberWrite = 'NaN';
        s.NullStringRead = 'null';
        s.NullStringWrite = 'null';
        s.JDBCDataSourceFile = '';
        s.UseRegistryForSources = 'yes';
        s.TempDirForRegistryOutput = '';
        conn = database('MSPM','root','admin');
        exec(conn,'TRUNCATE TABLE metals');
        % Store handle to connection in figure
        setappdata(f, 'dbconn', conn);

        % Set time to zero and store in figure
        time = 0;
        setappdata(f, 'time', time);

        % Create a timer object
        t = timer('ExecutionMode', 'fixedRate', 'Period', 2, 'BusyMode', 'drop',...
            'Tag', 'writeToDBtimer', 'TimerFcn', @writeToDBtimerfcn);

        % Gray out the start button
        startbut = findobj('tag', 'writeToDBstartbut');
        set(startbut, 'Enable', 'off');

        % Start the timer

%% Timer callback
        function writeToDBtimerfcn(hObject,eventdata) %#ok
            % Get handle to main figure
            f = findobj('Tag', 'writeToDBGUI');
            % Retrieve the time, data, and handle to the DB connection
            time = getappdata(f, 'time');
            conn = getappdata(f, 'dbconn');
            metals = getappdata(f, 'data');
            % Increase the time, looping round end of dataset
            time = mod(time,730)+1;
            % Write the latest measurement to the DB
%             fastinsert(conn,'metals',{'Ag','Ar','Bi','Ni','Pb','Sb','Se','Te'},...
%                 metals(time,:));
            sqlstring = 'INSERT INTO metals (Ag,Ar,Bi,Ni,Pb,Sb,Se,Te) VALUES (';
            for i = 1:8
                sqlstring = [sqlstring,num2str(metals(time,i)),','];
            sqlstring = [sqlstring(1:end-1),')'];

            % Report the write to the textbox
            textbox = findobj('Tag', 'writeToDBtextbox');
            string = sprintf('Time = %3.0f\nAg = %3.2f, As = %3.2f, Bi = %3.2f, Ni = %3.2f,\nPb = %3.2f, Sb = %3.2f, Se = %3.2f, Te = %3.2f',...
                time, metals(time,1),metals(time,2),metals(time,3),metals(time,4),...
            set(textbox, 'String', string)
            % Update the time to the figure
            setappdata(f, 'time', time);
        end % timer callback

    end % start button callback

%% Stop button callback
    function writeToDBstopfcn(hObject,eventdata) %#ok
        % Get handle to main figure
        f = findobj('Tag', 'writeToDBGUI');
        % Get handle to timer
        t = timerfind('Tag','writeToDBtimer');
        % Quit the timer
        % Clear the table and disconnect the DB connection
        conn = getappdata(f, 'dbconn');
%         curs = exec(conn,'TRUNCATE TABLE metals');
%         close(curs);
        % Close the figure
    end % stop button callback

end % main function

Contact us