source: trunk/src/uigetfile_uvmat.m @ 782

Last change on this file since 782 was 714, checked in by sommeria, 11 years ago

various improvements

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