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], 'Visible', 'off');
0086
0087 get(btn)
0088
0089
0090
0091
0092
0093 function qtxtCB(hObject, eventdata, query)
0094
0095 set(hObject, 'String', query);
0096
0097
0098
0099 function exportBtn_Callback(hObject, eventdata, gui, results, fields)
0100
0101
0102 t = getappdata(gui.main, 'table');
0103 table = get(t.Table);
0104
0105 rows = table.SelectedRows + 1;
0106 cols = table.SelectedColumns + 1;
0107 res = results(rows, cols);
0108 nResults = size(res, 1);
0109
0110
0111 [filename, pathname, filteridx] = uiputfile('*.txt', 'Select export file');
0112
0113 fd = fopen(filename, 'w+');
0114
0115
0116 fprintf(fd, '# ');
0117 fprintf(fd, '%s\t|\t', fields{cols});
0118 fprintf(fd, '\n');
0119
0120
0121 for j=1:nResults
0122 for k=1:size(res,2)
0123 r = res{j,k};
0124 if ischar(r)
0125 fprintf(fd, '%s\t\t', char(r));
0126 elseif isnumeric(r)
0127 fprintf(fd, '%f\t\t', r);
0128 else
0129 error('### Unknown data format.');
0130 end
0131 end
0132 fprintf(fd, '\n');
0133 end
0134 fclose(fd);
0135
0136 edit(filename);
0137
0138
0139
0140 function resizeMain(hObject, eventdata, leftMargin, bottomMargin, topMargin)
0141
0142
0143 s = getappdata(hObject, 'qstring');
0144 ps = get(s, 'Position');
0145 t = getappdata(hObject, 'table');
0146 fs = get(hObject, 'Position');
0147 pos = get(t, 'Position');
0148
0149
0150 set(s, 'Position', [ps(1) fs(4)-ps(4)-20 fs(3)-2*leftMargin ps(4)]);
0151
0152
0153