source: trunk/src/uigetfile_uvmat.m @ 641

Last change on this file since 641 was 641, checked in by sommeria, 8 years ago

corrections for mask and transform fct

File size: 15.3 KB
RevLine 
[606]1%'uigetfile_uvmat': browser faster than the Matlab fct uigetfile
2%------------------------------------------------------------------------
3% hfig=uigetfile_uvmat(OutputDir,option)
4%
5% OUTPUT:
[609]6% fileinput: detected file name, including path
[606]7%
8% INPUT:
[609]9% title: = displayed title, 'status': display advancement of a series calculation
[606]10% InputDir: directory to browse at first display
11
[641]12function fileinput=uigetfile_uvmat(title,InputName,FilterExt)
13if ~exist('FilterExt','var')
14    FilterExt='*';
15end
[609]16fileinput=''; %default file selection
[610]17if strcmp(title,'status_display')
18    option='status_display';
[609]19else
20    option='browser';
[606]21end
[615]22InputDir=pwd;%look in the current work directory if the input file does not exist
23InputFileName='';%default
24if 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
[610]36        end
[615]37        if isdir(InputName)
38            InputFileName=['+/' InputFileName Ext];
39        end
[610]40    end
[609]41end
42hfig=findobj(allchild(0),'tag',option);
[606]43if isempty(hfig)
[609]44    set(0,'Unit','points')
[606]45    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
[609]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);
[611]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');
[615]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,...
[611]55        'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold');
[612]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');   
[641]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',...
[609]65        'FontUnits','points','FontSize',12);
[611]66    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.005 0.2 0.07],'Callback',@(src,event)close(option,src,event),...
[609]67        'String','Close','FontWeight','bold','FontUnits','points','FontSize',12);
[611]68    uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.1 0.005 0.2 0.07],'Callback',@refresh_GUI,...
[609]69        'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12);
[606]70    %set(hrefresh,'UserData',StatusData)
[610]71    if strcmp(option,'status_display') %put a run advancement display
[635]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
[609]76    else  %put a title and additional pushbuttons
[612]77        uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.75 0.6 0.03],'BackgroundColor',BackgroundColor,...
[609]78            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
[612]79
[611]80        uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.005 0.2 0.07],...
[609]81            'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir);
[606]82    end
83    drawnow
84end
[641]85refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName,FilterExt)% refresh the list of content of the current dir 
[610]86if ~strcmp(option,'status_display') 
[609]87    uiwait(hfig)
[620]88    if ishandle(hfig)
[611]89    htitlebox=findobj(hfig,'Tag','titlebox');
90    fileinput=get(htitlebox,'String');% retrieve the input file selection
[610]91    delete(hfig)
[620]92    end
[609]93end
94
[615]95%------------------------------------------------------------------------   
96% --- launched by refreshing the display figure
97function titlebox_Callback(hObject,event)
98refresh_GUI(hObject)
99%------------------------------------------------------------------------
[610]100
[606]101%------------------------------------------------------------------------   
[610]102% --- launched by refreshing the display figure
[641]103function refresh_GUI(hObject,InputFileName,FilterExt)
[610]104%------------------------------------------------------------------------
105if ~exist('InputFileName','var')
106    InputFileName='';
107end
108hfig=get(hObject,'parent');
[613]109hlist=findobj(hfig,'tag','list');% find the list object
110set(hlist,'BackgroundColor',[1 1 0])
111drawnow
[611]112htitlebox=findobj(hfig,'tag','titlebox');
113DirName=get(htitlebox,'String');
[612]114hsort_option=findobj(hfig,'tag','sort_option');
[635]115if strcmp(get(hfig,'Tag'),'status_display')
[610]116    hseries=findobj(allchild(0),'tag','series');
[635]117    hstatus=findobj(hseries,'tag','status');
[610]118    StatusData=get(hstatus,'UserData');
119    TimeStart=0;
120    if isfield(StatusData,'TimeStart')
121        TimeStart=StatusData.TimeStart;
[635]122    end
[610]123    hlist=findobj(hfig,'tag','list');
[635]124    testrecent=0;   
[610]125    NbOutputFile=[];
[635]126    if isfield(StatusData,'NbOutputFile')
127        NbOutputFile=StatusData.NbOutputFile;
128        NbOutputFile_str=num2str(NbOutputFile);
[610]129    end
[641]130    [ListFiles,NumFiles]=list_files(DirName,1,TimeStart,FilterExt);% list the directory content
[610]131   
132    %% update the waitbar
133    hwaitbar=findobj(hfig,'tag','waitbar');
134    if ~isempty(NbOutputFile)
135        BarPosition=get(hwaitbar,'Position');
[635]136        BarPosition(3)=0.9*max(0.01,NumFiles/NbOutputFile);% the bar width cannot be set to 0, set to 0.01 instead
[610]137        set(hwaitbar,'Position',BarPosition)
138    end
[635]139else
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');
[641]145    [ListFiles,NumFiles]=list_files(DirName,get(hcheck_date,'Value'),sort_option,FilterExt);% list the directory content
[610]146end
147
[635]148set(hlist,'String',ListFiles)
149Value=[];
150if ~isempty(InputFileName)
151    Value=find(strcmp(InputFileName,ListFiles));
152end
153if isempty(Value)
154    Value=1;
155end
156set(hlist,'Value',Value)
157set(hlist,'BackgroundColor',[0.7 0.7 0.7])
[610]158%------------------------------------------------------------------------   
[609]159% --- launched by selecting an item on the file list
[612]160function dates_Callback(hObject,event)
[606]161%------------------------------------------------------------------------
[612]162hfig=get(hObject,'parent');
163hsort_option=findobj(hfig,'tag','sort_option');
164if get(hObject,'Value')
165    set(hsort_option,'Visible','on')
166    set(hsort_option,'Value',2)
167else
168    set(hsort_option,'Visible','off')
169end
170refresh_GUI(hObject,[])
171
172%------------------------------------------------------------------------   
173% --- launched by selecting an item on the file list
[641]174function list_Callback(option,filter_ext,hObject,event)
[612]175%------------------------------------------------------------------------
[617]176hfig=get(hObject,'parent');%handle of the fig
177% if ~strcmp(get(hfig,'SelectionType'),'open')
178%     return %select double click
179% end
180set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
[615]181    drawnow
[606]182list=get(hObject,'String');
183index=get(hObject,'Value');
[617]184
[611]185htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
186DirName=get(htitlebox,'String');
[609]187SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
[612]188ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
189if ~isempty(ind_dot)
190    SelectName=SelectName(1:ind_dot-1);
191end
[609]192if strcmp(SelectName,'..')% the upward dir option has been selected
[606]193    FullSelectName=fileparts(DirName);
194else
[609]195    FullSelectName=fullfile(DirName,SelectName);
[606]196end
197if exist(FullSelectName,'dir')% a directory has been selected
[612]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
[613]208    set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
209    drawnow
[609]210    hbackward=findobj(hfig,'Tag','backward');
211    set(hbackward,'UserData',DirName); %store the current dir for future backward action
[612]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');
[613]218   
[641]219    ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option,filter_ext);% list the directory content
[606]220    set(hObject,'Value',1)
221    set(hObject,'String',ListFiles)
[613]222    set(hObject,'BackgroundColor',[0.7 0.7 0.7])
[611]223    set(htitlebox,'String',FullSelectName)% record the new dir name
[606]224elseif 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)
[612]230    elseif strcmp(FileType,'figure')
[606]231        open(FullSelectName)
232    else
233        %uvmat(FullSelectName);
234        switch option
[609]235            case 'browser'
[612]236                set(htitlebox,'String',FullSelectName);
237                uiresume(hfig)
[610]238            case 'status_display'
[612]239                uvmat(FullSelectName);
[606]240        end
241    end
242end
[615]243set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end
[606]244
[610]245%-------------------------------------------------------------------------   
246% list the content of a directory
[641]247function [ListFiles,NumFiles]=list_files(DirName,check_date,sort_option,filter_ext)
[610]248%-------------------------------------------------------------------------
249ListStruct=dir(DirName);% get structure of the current directory
[635]250NumFiles=0; %default
[611]251if numel(ListStruct)<1  % case of empty dir
[610]252    ListFiles={};
253    return
254end
255ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
256ListFiles=ListCells(1,:);%list of file names
[611]257check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
[641]258
[610]259ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
[635]260ListDates=cell2mat(ListCells(5,:));%list of numerical dates
261if 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));
264end
265if ~isempty(find(~check_dir))
266ListDates(check_dir)=max(ListDates(~check_dir))+1000; % we set the dir in front
267end
[641]268
[635]269if isnumeric(sort_option)|| strcmp(sort_option,'date')
[612]270    [tild,index_sort]=sort(ListDates,2,'descend');% sort files by chronological order, recent first, put the dir first in the list
271else
272    [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list
273end
274ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
[610]275cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
[612]276check_keep=cellfun('isempty', cell_remove);
[641]277if exist('filter_ext')
278    switch filter_ext
279        case 'image'
280            check_keep=check_keep & (cellfun(@isimage,ListFiles) |check_dir);
281    end
282end
[610]283ListFiles=[{'+/..'} ListFiles(check_keep)];
[612]284if check_date
[635]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','...');
[612]294end
[610]295
[606]296%------------------------------------------------------------------------   
297% --- launched by selecting home
298function home_dir(hObject,event)
[609]299%------------------------------------------------------------------------
[606]300DirName=pwd;
301hfig=get(hObject,'parent');
[613]302hlist=findobj(hfig,'tag','list');% find the list object
303set(hlist,'BackgroundColor',[1 1 0])
304drawnow
[612]305sort_option='name';%default
306hsort_option=findobj(hfig,'tag','sort_option');
307if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
308    sort_option='date';
309end
310hcheck_date=findobj(hfig,'tag','check_date');
311ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content
[611]312htitlebox=findobj(hfig,'Tag','titlebox');
313set(htitlebox,'String',DirName)% record the new dir name
[613]314set(hlist,'Value',1)
[606]315set(hlist,'String',ListFiles)
[613]316set(hlist,'BackgroundColor',[0.7 0.7 0.7])
[606]317%------------------------------------------------------------------------
318
319%------------------------------------------------------------------------   
[609]320% --- launched by pressing the backward (<--) button
321function backward(hObject,event)
322%------------------------------------------------------------------------
323PrevDir=get(hObject,'UserData');
324if ~isempty(PrevDir)
325hfig=get(hObject,'parent');
326htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name
327set(htitlebox,'String',PrevDir)
328refresh_GUI(findobj(hfig,'Tag','refresh'))
329end
330
[606]331%-------------------------------------------------------------------------   
[641]332% --- launched by deleting the status figure (only used in mode series status')
333%-------------------------------------------------------------------------
[609]334function close(option,hObject, eventdata)
[641]335
[610]336if strcmp(option,'status_display')
[635]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])
[609]341end
[606]342delete(gcbf)
343
[641]344%-------------------------------------------------------------------------
345% --- check whether a file is has an image name extension
346%-------------------------------------------------------------------------
347function CheckImage=isimage(filename)
348
349[pp,name,ext]=fileparts(filename);
350ext
351CheckImage=~isempty(ext)&&~strcmp(ext,'.')&&~isempty(imformats(regexprep(ext,'^.','')));
Note: See TracBrowser for help on using the repository browser.