source: trunk/src/uigetfile_uvmat.m @ 807

Last change on this file since 807 was 803, checked in by sommeria, 10 years ago

bugs corrected in series and uigetfile_uvmat (for Windows OS)

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