0001 function sqlResultsGUI(results, fields, query)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 leftMargin = 10;
0013 bottomMargin = 100;
0014 topMargin = 20;
0015
0016 btnHeight = 20;
0017 btnWidth = 60;
0018 btnGap = 20;
0019
0020 Color = [240 240 240]/255;
0021 Height = 600;
0022 Width = 800;
0023
0024 Screen = get(0,'screensize');
0025 Position = [Screen(3)/2 - Width/2 Screen(4)/2-Height/2 Width Height];
0026
0027
0028 gui.main = figure('HandleVisibility','on',...
0029 'IntegerHandle','off',...
0030 'Menubar','none',...
0031 'NumberTitle','off',...
0032 'Name','Query Results',...
0033 'Tag','main',...
0034 'Color',Color,...
0035 'Resize', 'on',...
0036 'Position',Position,...
0037 'ResizeFcn', {@resizeMain, leftMargin, bottomMargin, topMargin});
0038
0039
0040 Tleft = leftMargin;
0041 Twidth = Width - 2*leftMargin;
0042 Theight = 80;
0043 Tbottom = Position(4) - Theight - 20;
0044 sth = uicontrol(gui.main,'Style','edit',...
0045 'String',query,...
0046 'Fontsize', 14,...
0047 'BackgroundColor','w',...
0048 'Max', 100,...
0049 'HorizontalAlignment', 'left',...
0050 'Tag', 'QueryString', ...
0051 'Enable', 'on', ...
0052 'Position',[Tleft Tbottom Twidth Theight], ...
0053 'Callback', {@qtxtCB, query});
0054
0055 setappdata(gui.main, 'qstring', sth);
0056
0057 if size(fields,2) ~= size(results,2)
0058 fields = cell(1,size(results,2));
0059 end
0060
0061
0062 v = ver('MATLAB');
0063 if strcmp(v.Version, '7.6')
0064 t = uitable(gui.main, 'Data', results, ...
0065 'ColumnName', fields,...
0066 'units', 'normalized',...
0067 'Position', [.01,.08,1-0.02,.73]);
0068 else
0069
0070
0071 [t,b] = createTable(gui.main, fields, results, false, ...
0072 'AutoResizeMode',javax.swing.JTable.AUTO_RESIZE_OFF,...
0073 'Editable',false,'Position',[.01,.08,1-0.02,.73]);
0074
0075 end
0076
0077 setappdata(gui.main, 'table', t);
0078
0079
0080 btn = uicontrol(gui.main,...
0081 'Style','pushbutton',...
0082 'String','Export',...
0083 'Tag', 'exportBtn',...
0084 'Callback', {@exportBtn_Callback, gui, results, fields}, ...
0085 'Position',[leftMargin leftMargin btnWidth btnHeight]);
0086
0087
0088
0089
0090
0091 function qtxtCB(hObject, eventdata, query)
0092
0093 set(hObject, 'String', query);
0094
0095
0096
0097 function exportBtn_Callback(hObject, eventdata, gui, results, fields)
0098
0099
0100 t = getappdata(gui.main, 'table');
0101 table = get(t.Table);
0102
0103 rows = table.SelectedRows + 1;
0104 cols = table.SelectedColumns + 1;
0105 res = results(rows, cols);
0106 nResults = size(res, 1);
0107
0108
0109 [filename, pathname, filteridx] = uiputfile('*.txt', 'Select export file');
0110
0111 fd = fopen(filename, 'w+');
0112
0113
0114 fprintf(fd, '# ');
0115 fprintf(fd, '%s\t|\t', fields{cols});
0116 fprintf(fd, '\n');
0117
0118
0119 for j=1:nResults
0120 for k=1:size(res,2)
0121 r = res{j,k};
0122 if ischar(r)
0123 fprintf(fd, '%s\t\t', char(r));
0124 elseif isnumeric(r)
0125 fprintf(fd, '%f\t\t', r);
0126 else
0127 error('### Unknown data format.');
0128 end
0129 end
0130 fprintf(fd, '\n');
0131 end
0132 fclose(fd);
0133
0134 edit(filename);
0135
0136
0137
0138 function resizeMain(hObject, eventdata, leftMargin, bottomMargin, topMargin)
0139
0140
0141 s = getappdata(hObject, 'qstring');
0142 ps = get(s, 'Position');
0143 t = getappdata(hObject, 'table');
0144 fs = get(hObject, 'Position');
0145 pos = get(t, 'Position');
0146
0147
0148 set(s, 'Position', [ps(1) fs(4)-ps(4)-20 fs(3)-2*leftMargin ps(4)]);
0149
0150
0151