source: trunk/src/uigetfile_uvmat.m @ 686

Last change on this file since 686 was 664, checked in by sommeria, 11 years ago

a few bugs corrected: import and retrival of parameters in series. mask in civ_series

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