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