source: trunk/src/uigetfile_uvmat.m @ 620

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

bugs repaired in series + cleaning.
Introducing of a demo program (test stage)

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