1 | %'uigetfile_uvmat': browser faster than the Matlab fct uigetfile |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | % hfig=uigetfile_uvmat(OutputDir,option) |
---|
4 | % |
---|
5 | % OUTPUT: |
---|
6 | % fileinput: detected file name, including path |
---|
7 | % |
---|
8 | % INPUT: |
---|
9 | % title: = displayed title, 'status': display advancement of a series calculation |
---|
10 | % InputDir: directory to browse at first display |
---|
11 | |
---|
12 | function fileinput=uigetfile_uvmat(title,InputName) |
---|
13 | fileinput=''; %default file selection |
---|
14 | if strcmp(title,'status_display') |
---|
15 | option='status_display'; |
---|
16 | else |
---|
17 | option='browser'; |
---|
18 | end |
---|
19 | InputDir=pwd;%look in the current work directory if the input file does not exist |
---|
20 | InputFileName='';%default |
---|
21 | if ischar(InputName) |
---|
22 | if exist(InputName,'dir') |
---|
23 | InputDir=InputName; |
---|
24 | InputFileName=''; |
---|
25 | elseif exist(InputName,'file') |
---|
26 | [InputDir,InputFileName,Ext]=fileparts(InputName); |
---|
27 | if isempty(InputFileName)% if InputName is already the root |
---|
28 | InputFileName=InputDir; |
---|
29 | if ~isempty(strcmp (computer, {'PCWIN','PCWIN64'}))%case of Windows systems |
---|
30 | InputDir=[InputDir '\'];% append '\' for a correct action of dir |
---|
31 | InputFileName=[InputFileName '\']; |
---|
32 | end |
---|
33 | end |
---|
34 | if isdir(InputName) |
---|
35 | InputFileName=['+/' InputFileName Ext]; |
---|
36 | end |
---|
37 | end |
---|
38 | end |
---|
39 | hfig=findobj(allchild(0),'tag',option); |
---|
40 | if isempty(hfig) |
---|
41 | set(0,'Unit','points') |
---|
42 | ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right |
---|
43 | Width=350;% fig width in points (1/72 inch) |
---|
44 | Height=min(0.8*ScreenSize(4),500); |
---|
45 | Left=ScreenSize(3)- Width-40; %right edge close to the right, with margin=40 |
---|
46 | Bottom=ScreenSize(4)-Height-40; %put fig at top right |
---|
47 | hfig=figure('name',option,'tag',option,'MenuBar','none','NumberTitle','off','Unit','points','Position',[Left,Bottom,Width,Height],'UserData',InputDir); |
---|
48 | BackgroundColor=get(hfig,'Color'); |
---|
49 | uicontrol('Style','text','Units','normalized', 'Position', [0.05 0.97 0.5 0.03],'BackgroundColor',BackgroundColor,... |
---|
50 | 'String','path:','FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left'); |
---|
51 | uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.89 0.9 0.08],'tag','titlebox','Max',2,'BackgroundColor',[1 1 1],'Callback',@titlebox_Callback,... |
---|
52 | 'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold'); |
---|
53 | uicontrol('Style','pushbutton','Tag','backward','Units','normalized','Position',[0.05 0.75 0.1 0.07],... |
---|
54 | 'String','<--','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@backward); |
---|
55 | uicontrol('Style','togglebutton','Units','normalized', 'Position', [0.75 0.75 0.2 0.04],'tag','check_date','Callback',@dates_Callback,... |
---|
56 | 'String','dates','FontUnits','points','FontSize',12,'FontWeight','bold'); |
---|
57 | uicontrol('Style','text','Units','normalized', 'Position', [0.4 0.8 0.35 0.03],'BackgroundColor',BackgroundColor,... |
---|
58 | 'String','sort: ','FontUnits','points','FontSize',12,'FontWeight','bold','HorizontalAlignment','right'); |
---|
59 | uicontrol('Style','popupmenu','Units','normalized', 'Position', [0.75 0.8 0.2 0.04],'tag','sort_option','Callback',@refresh_GUI,'Visible','off',... |
---|
60 | 'String',{'name';'date'},'FontUnits','points','FontSize',12,'FontWeight','bold'); |
---|
61 | uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.08 0.9 0.66], 'Callback', @(src,event)list_Callback(option,src,event),'tag','list',... |
---|
62 | 'FontUnits','points','FontSize',12); |
---|
63 | uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.005 0.2 0.07],'Callback',@(src,event)close(option,src,event),... |
---|
64 | 'String','Close','FontWeight','bold','FontUnits','points','FontSize',12); |
---|
65 | uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.1 0.005 0.2 0.07],'Callback',@refresh_GUI,... |
---|
66 | 'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12); |
---|
67 | %set(hrefresh,'UserData',StatusData) |
---|
68 | if strcmp(option,'status_display') %put a run advancement display |
---|
69 | set(hfig,'DeleteFcn',@stop_status) |
---|
70 | uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.81 0.01 0.05],'BackgroundColor',[1 0 0],'tag','waitbar'); |
---|
71 | uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]); |
---|
72 | else %put a title and additional pushbuttons |
---|
73 | uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.75 0.6 0.03],'BackgroundColor',BackgroundColor,... |
---|
74 | 'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left'); |
---|
75 | |
---|
76 | uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.005 0.2 0.07],... |
---|
77 | 'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir); |
---|
78 | end |
---|
79 | drawnow |
---|
80 | end |
---|
81 | refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName)% refresh the list of content of the current dir |
---|
82 | if ~strcmp(option,'status_display') |
---|
83 | uiwait(hfig) |
---|
84 | htitlebox=findobj(hfig,'Tag','titlebox'); |
---|
85 | fileinput=get(htitlebox,'String');% retrieve the input file selection |
---|
86 | delete(hfig) |
---|
87 | end |
---|
88 | |
---|
89 | %------------------------------------------------------------------------ |
---|
90 | % --- launched by refreshing the display figure |
---|
91 | function titlebox_Callback(hObject,event) |
---|
92 | refresh_GUI(hObject) |
---|
93 | %------------------------------------------------------------------------ |
---|
94 | |
---|
95 | %------------------------------------------------------------------------ |
---|
96 | % --- launched by refreshing the display figure |
---|
97 | function refresh_GUI(hObject,InputFileName) |
---|
98 | %------------------------------------------------------------------------ |
---|
99 | if ~exist('InputFileName','var') |
---|
100 | InputFileName=''; |
---|
101 | end |
---|
102 | hfig=get(hObject,'parent'); |
---|
103 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
104 | set(hlist,'BackgroundColor',[1 1 0]) |
---|
105 | drawnow |
---|
106 | htitlebox=findobj(hfig,'tag','titlebox'); |
---|
107 | DirName=get(htitlebox,'String'); |
---|
108 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
109 | sort_option='name'; |
---|
110 | if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2) |
---|
111 | sort_option='date'; |
---|
112 | end |
---|
113 | hcheck_date=findobj(hfig,'tag','check_date'); |
---|
114 | ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content |
---|
115 | |
---|
116 | set(hlist,'String',ListFiles) |
---|
117 | Value=[]; |
---|
118 | if ~isempty(InputFileName) |
---|
119 | Value=find(strcmp(InputFileName,ListFiles)); |
---|
120 | end |
---|
121 | if isempty(Value) |
---|
122 | Value=1; |
---|
123 | end |
---|
124 | set(hlist,'Value',Value) |
---|
125 | set(hlist,'BackgroundColor',[0.7 0.7 0.7]) |
---|
126 | if strcmp(get(hfig,'Tag'),'status_display') |
---|
127 | hseries=findobj(allchild(0),'tag','series'); |
---|
128 | hstatus=findobj(hseries,'tag','status_display'); |
---|
129 | StatusData=get(hstatus,'UserData'); |
---|
130 | TimeStart=0; |
---|
131 | if isfield(StatusData,'TimeStart') |
---|
132 | TimeStart=StatusData.TimeStart; |
---|
133 | end |
---|
134 | hlist=findobj(hfig,'tag','list'); |
---|
135 | testrecent=0; |
---|
136 | datnum=zeros(numel(ListDisplay),1); |
---|
137 | for ilist=1:numel(ListDisplay) |
---|
138 | ListDisplay{ilist}=ListFiles(ilist).name; |
---|
139 | if ListFiles(ilist).isdir |
---|
140 | ListDisplay{ilist}=['/' ListDisplay{ilist}]; |
---|
141 | elseif isfield(ListFiles(ilist),'datenum') |
---|
142 | datnum(ilist)=ListFiles(ilist).datenum;%only available in recent matlab versions |
---|
143 | testrecent=1; |
---|
144 | if datnum(ilist)<TimeStart |
---|
145 | ListDisplay{ilist}=[ListDisplay{ilist} ' --OLD--']; |
---|
146 | end |
---|
147 | end |
---|
148 | end |
---|
149 | |
---|
150 | %% Look at date of creation |
---|
151 | ListDisplay=ListDisplay(datnum~=0); |
---|
152 | datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files |
---|
153 | |
---|
154 | NbOutputFile=[]; |
---|
155 | if isempty(datnum) |
---|
156 | if testrecent |
---|
157 | message='no result created yet'; |
---|
158 | else |
---|
159 | message=''; |
---|
160 | end |
---|
161 | else |
---|
162 | [first,indfirst]=min(datnum); |
---|
163 | [last,indlast]=max(datnum); |
---|
164 | NbOutputFile_str='?'; |
---|
165 | if isfield(StatusData,'NbOutputFile') |
---|
166 | NbOutputFile=StatusData.NbOutputFile; |
---|
167 | NbOutputFile_str=num2str(NbOutputFile); |
---|
168 | end |
---|
169 | message={[num2str(numel(datnum)) ' file(s) done over ' NbOutputFile_str] ;['oldest modification: ' ListDisplay{indfirst} ' : ' datestr(first)];... |
---|
170 | ['latest modification: ' ListDisplay{indlast} ' : ' datestr(last)]}; |
---|
171 | end |
---|
172 | set(htitlebox,'String', [DirName{1};message]) |
---|
173 | |
---|
174 | %% update the waitbar |
---|
175 | hwaitbar=findobj(hfig,'tag','waitbar'); |
---|
176 | if ~isempty(NbOutputFile) |
---|
177 | BarPosition=get(hwaitbar,'Position'); |
---|
178 | BarPosition(3)=0.9*numel(datnum)/NbOutputFile; |
---|
179 | set(hwaitbar,'Position',BarPosition) |
---|
180 | end |
---|
181 | end |
---|
182 | |
---|
183 | %------------------------------------------------------------------------ |
---|
184 | % --- launched by selecting an item on the file list |
---|
185 | function dates_Callback(hObject,event) |
---|
186 | %------------------------------------------------------------------------ |
---|
187 | hfig=get(hObject,'parent'); |
---|
188 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
189 | if get(hObject,'Value') |
---|
190 | set(hsort_option,'Visible','on') |
---|
191 | set(hsort_option,'Value',2) |
---|
192 | else |
---|
193 | set(hsort_option,'Visible','off') |
---|
194 | end |
---|
195 | refresh_GUI(hObject,[]) |
---|
196 | |
---|
197 | %------------------------------------------------------------------------ |
---|
198 | % --- launched by selecting an item on the file list |
---|
199 | function list_Callback(option,hObject,event) |
---|
200 | %------------------------------------------------------------------------ |
---|
201 | hfig=get(hObject,'parent');%handle of the fig |
---|
202 | % if ~strcmp(get(hfig,'SelectionType'),'open') |
---|
203 | % return %select double click |
---|
204 | % end |
---|
205 | set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action |
---|
206 | drawnow |
---|
207 | list=get(hObject,'String'); |
---|
208 | index=get(hObject,'Value'); |
---|
209 | |
---|
210 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
211 | DirName=get(htitlebox,'String'); |
---|
212 | SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir |
---|
213 | ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond '...' |
---|
214 | if ~isempty(ind_dot) |
---|
215 | SelectName=SelectName(1:ind_dot-1); |
---|
216 | end |
---|
217 | if strcmp(SelectName,'..')% the upward dir option has been selected |
---|
218 | FullSelectName=fileparts(DirName); |
---|
219 | else |
---|
220 | FullSelectName=fullfile(DirName,SelectName); |
---|
221 | end |
---|
222 | if exist(FullSelectName,'dir')% a directory has been selected |
---|
223 | % ListFiles=dir(FullSelectName); |
---|
224 | % ListDisplay=cell(numel(ListFiles),1); |
---|
225 | % for ilist=2:numel(ListDisplay)% suppress the first line '.' |
---|
226 | % ListDisplay{ilist-1}=ListFiles(ilist).name; |
---|
227 | % end |
---|
228 | % set(hObject,'Value',1) |
---|
229 | % set(hObject,'String',ListDisplay) |
---|
230 | % if strcmp(selectname,'..') |
---|
231 | % FullSelectName=fileparts(fileparts(FullSelectName)); |
---|
232 | % end |
---|
233 | set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action |
---|
234 | drawnow |
---|
235 | hbackward=findobj(hfig,'Tag','backward'); |
---|
236 | set(hbackward,'UserData',DirName); %store the current dir for future backward action |
---|
237 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
238 | sort_option='name';%default |
---|
239 | if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2) |
---|
240 | sort_option='date'; |
---|
241 | end |
---|
242 | hcheck_date=findobj(hfig,'tag','check_date'); |
---|
243 | |
---|
244 | ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option);% list the directory content |
---|
245 | set(hObject,'Value',1) |
---|
246 | set(hObject,'String',ListFiles) |
---|
247 | set(hObject,'BackgroundColor',[0.7 0.7 0.7]) |
---|
248 | set(htitlebox,'String',FullSelectName)% record the new dir name |
---|
249 | elseif exist(FullSelectName,'file')%visualise the field if it exists |
---|
250 | FileType=get_file_type(FullSelectName); |
---|
251 | if strcmp(FileType,'txt') |
---|
252 | edit(FullSelectName) |
---|
253 | elseif strcmp(FileType,'xml') |
---|
254 | editxml(FullSelectName) |
---|
255 | elseif strcmp(FileType,'figure') |
---|
256 | open(FullSelectName) |
---|
257 | else |
---|
258 | %uvmat(FullSelectName); |
---|
259 | switch option |
---|
260 | case 'browser' |
---|
261 | set(htitlebox,'String',FullSelectName); |
---|
262 | uiresume(hfig) |
---|
263 | case 'status_display' |
---|
264 | uvmat(FullSelectName); |
---|
265 | end |
---|
266 | end |
---|
267 | end |
---|
268 | set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end |
---|
269 | |
---|
270 | %------------------------------------------------------------------------- |
---|
271 | % list the content of a directory |
---|
272 | function ListFiles=list_files(DirName,check_date,sort_option) |
---|
273 | %------------------------------------------------------------------------- |
---|
274 | ListStruct=dir(DirName);% get structure of the current directory |
---|
275 | if numel(ListStruct)<1 % case of empty dir |
---|
276 | ListFiles={}; |
---|
277 | return |
---|
278 | end |
---|
279 | ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray |
---|
280 | ListFiles=ListCells(1,:);%list of file names |
---|
281 | check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files |
---|
282 | ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display |
---|
283 | if strcmp(sort_option,'date') |
---|
284 | ListDates=cell2mat(ListCells(5,:));%list of numerical dates |
---|
285 | ListDates(check_dir)=max(ListDates(~check_dir))+1000; % we set the dir in front |
---|
286 | [tild,index_sort]=sort(ListDates,2,'descend');% sort files by chronological order, recent first, put the dir first in the list |
---|
287 | else |
---|
288 | [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list |
---|
289 | end |
---|
290 | ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file |
---|
291 | cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . ) |
---|
292 | check_keep=cellfun('isempty', cell_remove); |
---|
293 | ListFiles=[{'+/..'} ListFiles(check_keep)]; |
---|
294 | if check_date |
---|
295 | ListDateString=ListCells(2,:);%list of file dates |
---|
296 | ListDateString(check_dir)={''}; |
---|
297 | ListDateString=ListDateString(index_sort);% sort the corresponding dates |
---|
298 | ListDateString=[{''} ListDateString(check_keep)]; |
---|
299 | ListFiles=[ListFiles; ListDateString]; |
---|
300 | ListFiles=cell2tab(ListFiles','...'); |
---|
301 | end |
---|
302 | |
---|
303 | %------------------------------------------------------------------------ |
---|
304 | % --- launched by selecting home |
---|
305 | function home_dir(hObject,event) |
---|
306 | %------------------------------------------------------------------------ |
---|
307 | DirName=pwd; |
---|
308 | hfig=get(hObject,'parent'); |
---|
309 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
310 | set(hlist,'BackgroundColor',[1 1 0]) |
---|
311 | drawnow |
---|
312 | sort_option='name';%default |
---|
313 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
314 | if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2) |
---|
315 | sort_option='date'; |
---|
316 | end |
---|
317 | hcheck_date=findobj(hfig,'tag','check_date'); |
---|
318 | ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content |
---|
319 | htitlebox=findobj(hfig,'Tag','titlebox'); |
---|
320 | set(htitlebox,'String',DirName)% record the new dir name |
---|
321 | set(hlist,'Value',1) |
---|
322 | set(hlist,'String',ListFiles) |
---|
323 | set(hlist,'BackgroundColor',[0.7 0.7 0.7]) |
---|
324 | %------------------------------------------------------------------------ |
---|
325 | |
---|
326 | %------------------------------------------------------------------------ |
---|
327 | % --- launched by pressing the backward (<--) button |
---|
328 | function backward(hObject,event) |
---|
329 | %------------------------------------------------------------------------ |
---|
330 | PrevDir=get(hObject,'UserData'); |
---|
331 | if ~isempty(PrevDir) |
---|
332 | hfig=get(hObject,'parent'); |
---|
333 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
334 | set(htitlebox,'String',PrevDir) |
---|
335 | refresh_GUI(findobj(hfig,'Tag','refresh')) |
---|
336 | end |
---|
337 | |
---|
338 | %------------------------------------------------------------------------- |
---|
339 | % launched by deleting the status figure (only used in mode series status') |
---|
340 | function close(option,hObject, eventdata) |
---|
341 | %------------------------------------------------------------------------- |
---|
342 | if strcmp(option,'status_display') |
---|
343 | hseries=findobj(allchild(0),'tag','series'); |
---|
344 | hstatus=findobj(hfig,'Tag','status_display'); |
---|
345 | set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ |
---|
346 | set(hhciv.status,'BackgroundColor',[0 1 0]) |
---|
347 | end |
---|
348 | delete(gcbf) |
---|
349 | |
---|