function varargout = AntoineDB_GUI(varargin)
% ANTOINEDB_GUI MATLAB code for AntoineDB_GUI.fig
% This is a standalone GUI that graphs and calculates the vapor
% pressure of various chemical compounds over the allowed temperature
% range. It utilizes the antoine database containing over 650
% compounds.
%
% (c) David Hagan 2012
%
% Updates:
% (1) July 21,2012 -> edited DB to include over 4000
% Compounds. Added option to change Pressure Unit on the
% y-axis and to change the input Temp. Unit
%
% Last Modified by GUIDE v2.5 21-Jul-2012 16:52:31
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @AntoineDB_GUI_OpeningFcn, ...
'gui_OutputFcn', @AntoineDB_GUI_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
end
% --- Executes just before AntoineDB_GUI is made visible.
function AntoineDB_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
set(handles.mmHg,'Value',1); % sets the default pressure unit to mm Hg
set(handles.bar,'Value',0);
set(handles.atm,'Value',0);
set(handles.MPa,'Value',0);
set(handles.Tcelcius,'Value',1);
set(handles.Tkelvin,'Value',0);
handles.Tunit = 'C';
load_listbox(hObject,handles); %load the data into the listbox
handles.output = hObject; % Choose default command line output for AntoineDB_GUI
guidata(hObject, handles); % Update handles structure
end
% --- Outputs from this function are returned to the command line.
function varargout = AntoineDB_GUI_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output; % Get default command line output from handles structure
end
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
index_selected = get(hObject,'Value');
list = get(hObject,'String');
realID = getRealID(index_selected);
[formula,name,A,B,C,Tmin,Tmax] = grabData(realID,handles,hObject);
handles.Formula = formula;
handles.Name = name;
handles.A = A;
handles.B = B;
handles.C = C;
handles.Tmin = Tmin;
handles.Tmax = Tmax;
handles.output = hObject; % load_listbox(handles);
guidata(hObject, handles); % Update handles structure
end
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
handles.output = hObject;
guidata(hObject, handles);
end
% --- Executes upon loading the GUI -> populates the listbox with values
% from the antoine database
function load_listbox(hObject,handles)
conn = database('chemical_info','','');
setdbprefs('datareturnformat','structure'); %sets the db preferences to a structure
query = 'SELECT ALL ID,Name FROM chemical_info_1 ORDER BY Name';
result = fetch(conn,query);
%The following creates a structure containing the names and ID's
%of everything in the database
data = struct([]);
for i=1:length(result.ID)
data(i).id = result.ID(i);
data(i).name = (result.Name(i)); %this is a cell
names(i) = data(i).name;
end
handles.compounds = names;
set(handles.listbox1,'String',handles.compounds,'Value',1); %sets the listbox text to names
handles.output = hObject;
guidata(hObject, handles); % Update handles structure
end
%-- This function takes the listID and finds its correct ID in the db
function [realID] = getRealID(listID)
conn = database('chemical_info','','');
setdbprefs('datareturnformat','structure'); %sets the db preferences to a structure
query = 'SELECT ALL ID,Name FROM chemical_info_1 ORDER BY Name';
result = fetch(conn,query);
%The following creates a structure containing the names and ID's
%of everything in the database
data = struct([]);
for i=1:length(result.ID)
data(i).id = result.ID(i);
data(i).name = (result.Name(i)); %this is a cell
names(i) = data(i).name;
end
realID = data(listID).id;
end
% -- grabData is executed once an option is chosen in the listbox
function [formula,name,A,B,C,Tmin,Tmax] = grabData(ID,handles,hObject)
conn = database('chemical_info','','');
setdbprefs('datareturnformat','structure'); %sets the db preferences to a structure
query = ['SELECT ALL ID,Name,CAS,Formula,A,B,C,Tmin,Tmax FROM chemical_info_1 WHERE ID=' num2str(ID)]; % num2str(ID)
result = fetch(conn,query); %fieldnames(result)
formula = char(result.Formula);
name = char(result.Name);
A = result.A;
B = result.B;
C = result.C;
Tmin = result.Tmin;
Tmax = result.Tmax;
handles.output = hObject;
guidata(hObject, handles); % Update handles structure
end
% --- Executes on button press in plot.
function plot_Callback(hObject, eventdata, handles)
P = handles.Punit;
try
j = handles.Tmin:1:handles.Tmax; %creates an array containing temps between the defined limits
pvap = antoine(handles.A,handles.B,handles.C,j); %calculates P (mmHg) at each Temp (C)
% Check the Pressure units and convert if neccesary
switch P
case 'mmHg'
pvap = pvap;
case 'bar'
pvap = mmHgToBar(pvap);
case 'MPa'
pvap = mmHgToMPa(pvap);
case 'atm'
pvap = mmHgToAtm(pvap);
end
cla %clears the axes and resets the graphing window
hold on
title(['Vapor Pressure vs. Temperature for ', handles.Name]);
xlabel('Temperature (oC)');
ylabel(['Pressure (',handles.Punit,')']);
plot(j,pvap,'r');
hold off
catch
errordlg('Please make sure a compound is selected. ');
end
handles.output = hObject;
guidata(hObject, handles); % Update handles structure
end
%-- This is the function that defines the Antoine equation
function [VP] = antoine(A,B,C,T)
VP = 10.^(A - B./(T + C));
end
function temp_Callback(hObject, eventdata, handles)
end
% --- Executes during object creation, after setting all properties.
function temp_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
function P_Callback(hObject, eventdata, handles)
end
% --- Executes during object creation, after setting all properties.
function P_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes on button press in calcVP.
function calcVP_Callback(hObject, eventdata, handles)
T = str2num(get(handles.temp,'string'));
Tunit = handles.Tunit;
switch Tunit
case 'K'
T = T - 273.15;
case 'C'
T = T;
end
%--Check to make sure the temperature entered is within the limits
if (T >= handles.Tmin && T <= handles.Tmax)
pressure = antoine(handles.A,handles.B,handles.C,T);
set(handles.P,'string',num2str(pressure));
else
errordlg('The temperature you entered is outside the allowed range');
end
end
% --- Executes on button press in mmHg.
function mmHg_Callback(hObject, eventdata, handles)
% hObject handle to mmHg (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if (get(hObject,'Value') == 1)
set(handles.bar,'Value',0);
set(handles.MPa,'Value',0);
set(handles.atm,'Value',0);
handles.Punit = 'mmHg';
end
handles.output = hObject;
guidata(hObject, handles);
end
% --- Executes on button press in bar.
function bar_Callback(hObject, eventdata, handles)
% hObject handle to bar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if (get(hObject,'Value') == 1)
set(handles.mmHg,'Value',0);
set(handles.MPa,'Value',0);
set(handles.atm,'Value',0);
handles.Punit = 'bar';
end
handles.output = hObject;
guidata(hObject, handles);
end
% --- Executes on button press in atm.
function atm_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == 1)
set(handles.mmHg,'Value',0);
set(handles.MPa,'Value',0);
set(handles.bar,'Value',0);
handles.Punit = 'atm';
end
handles.output = hObject;
guidata(hObject, handles);
end
% --- Executes on button press in MPa.
function MPa_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == 1)
set(handles.mmHg,'Value',0);
set(handles.bar,'Value',0);
set(handles.atm,'Value',0);
handles.Punit = 'MPa';
end
handles.output = hObject;
guidata(hObject, handles);
end
function vp = mmHgToBar(pvap)
vp = pvap.*(1.01325./760);
end
function vp = mmHgToMPa(pvap)
vp = pvap.*(1.01325./760);
end
function vp = mmHgToAtm(pvap)
vp = pvap.*(1./760);
end
% --- Executes on button press in Tkelvin.
function Tkelvin_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == 1)
set(handles.Tcelcius,'Value',0);
handles.Tunit = 'K';
end
handles.output = hObject;
guidata(hObject, handles);
end
% --- Executes on button press in Tcelcius.
function Tcelcius_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == 1)
set(handles.Tkelvin,'Value',0);
handles.Tunit = 'C';
end
handles.output = hObject;
guidata(hObject, handles);
end