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
0056 if size(fields,2) ~= size(results,2)
0057 fields = cell(1,size(results,2));
0058 end
0059
0060
0061
0062
0063
0064
0065
0066 Tleft = leftMargin;
0067 Twidth = Width - 2*leftMargin;
0068 Theight = 500;
0069 Tbottom = leftMargin + btnHeight + 20;
0070
0071
0072
0073
0074
0075
0076 t = muitable(gui.main, results, fields, 'Position',...
0077 [Tleft Tbottom Twidth Theight]);
0078 set(t, 'Editable', 0);
0079
0080 t.Table.setAutoResizeMode(t.Table.AUTO_RESIZE_OFF);
0081
0082
0083
0084
0085 setappdata(gui.main, 'table', t);
0086
0087
0088 btn = uicontrol(gui.main,...
0089 'Style','pushbutton',...
0090 'String','Export',...
0091 'Tag', 'exportBtn',...
0092 'Callback', {@exportBtn_Callback, gui, results, fields}, ...
0093 'Position',[leftMargin leftMargin btnWidth btnHeight]);
0094
0095
0096
0097
0098
0099 function qtxtCB(hObject, eventdata, query)
0100
0101 set(hObject, 'String', query);
0102
0103
0104
0105 function exportBtn_Callback(hObject, eventdata, gui, results, fields)
0106
0107
0108 t = getappdata(gui.main, 'table');
0109 table = get(t.Table);
0110
0111 rows = table.SelectedRows + 1;
0112 cols = table.SelectedColumns + 1;
0113 res = results(rows, cols);
0114 nResults = size(res, 1);
0115
0116
0117 [filename, pathname, filteridx] = uiputfile('*.txt', 'Select export file');
0118
0119 fd = fopen(filename, 'w+');
0120
0121
0122 fprintf(fd, '# ');
0123 fprintf(fd, '%s\t|\t', fields{cols});
0124 fprintf(fd, '\n');
0125
0126
0127 for j=1:nResults
0128 for k=1:size(res,2)
0129 r = res{j,k};
0130 if ischar(r)
0131 fprintf(fd, '%s\t\t', char(r));
0132 elseif isnumeric(r)
0133 fprintf(fd, '%f\t\t', r);
0134 else
0135 error('### Unknown data format.');
0136 end
0137 end
0138 fprintf(fd, '\n');
0139 end
0140 fclose(fd);
0141
0142 edit(filename);
0143
0144
0145
0146 function resizeMain(hObject, eventdata, leftMargin, bottomMargin, topMargin)
0147
0148
0149 s = findobj('Tag', 'QueryString');
0150 ps = get(s, 'Position');
0151
0152 fs = get(hObject, 'Position');
0153
0154
0155
0156 set(s, 'Position', [ps(1) fs(4)-ps(4)-20 fs(3)-2*leftMargin ps(4)]);
0157
0158
0159