[606] | 1 | %'uigetfile_uvmat': browser faster than the Matlab fct uigetfile |
---|
| 2 | %------------------------------------------------------------------------ |
---|
| 3 | % hfig=uigetfile_uvmat(OutputDir,option) |
---|
| 4 | % |
---|
| 5 | % OUTPUT: |
---|
| 6 | % hfig: handles of the browser fig, the selected file name is obtained as File=get(hfig,'UserData') |
---|
| 7 | % |
---|
| 8 | % INPUT: |
---|
| 9 | % option: ='file browser': usual browser, 'series status': display advancement of a series calculation |
---|
| 10 | % InputDir: directory to browse at first display |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | function hfig=uigetfile_uvmat(option,InputDir) |
---|
| 14 | if ~exist(InputDir,'dir') |
---|
| 15 | InputDir=pwd; |
---|
| 16 | end |
---|
| 17 | hfig=findobj(allchild(0),'name',option); |
---|
| 18 | if isempty(hfig) |
---|
| 19 | ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right |
---|
| 20 | hfig=figure('name',option,'tag',option,'MenuBar','none','NumberTitle','off','Position',[ScreenSize(3)-600 ScreenSize(4)-640 560 600],'DeleteFcn',@stop_status,'UserData',InputDir); |
---|
| 21 | uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', @(src,event)view_file(option,src,event),'tag','list','FontSize',12); |
---|
| 22 | uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','titlebox','Max',2,'String',InputDir,'FontSize',12,'FontWeight','bold'); |
---|
| 23 | uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.01 0.2 0.07],'String','Close','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@stop_status); |
---|
| 24 | uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.1 0.01 0.2 0.07],'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@refresh_GUI); |
---|
| 25 | %set(hrefresh,'UserData',StatusData) |
---|
| 26 | if strcmp(option,'series status') %put a run advancement display |
---|
| 27 | uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.81 0.01 0.05],'BackgroundColor',[1 0 0],'tag','waitbar'); |
---|
| 28 | uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]); |
---|
| 29 | else %put a title |
---|
| 30 | uicontrol('Style','text','Units','normalized', 'Position', [0.05 0.81 0.9 0.03],'String','select an input file:',... |
---|
| 31 | 'FontSize',14,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left'); |
---|
| 32 | uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.01 0.2 0.07],'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir); |
---|
| 33 | end |
---|
| 34 | drawnow |
---|
| 35 | end |
---|
| 36 | refresh_GUI(hfig) |
---|
| 37 | |
---|
| 38 | %------------------------------------------------------------------------ |
---|
| 39 | % --- launched by selecting a file on the list |
---|
| 40 | function view_file(option,hObject,event) |
---|
| 41 | %------------------------------------------------------------------------ |
---|
| 42 | list=get(hObject,'String'); |
---|
| 43 | index=get(hObject,'Value'); |
---|
| 44 | hfig=get(hObject,'parent'); |
---|
| 45 | DirName=get(get(hObject,'parent'),'UserData'); |
---|
| 46 | SelectName=regexprep(list{index},'^/','');% remove the / used to mark dir |
---|
| 47 | if strcmp(SelectName,'..') |
---|
| 48 | FullSelectName=fileparts(DirName); |
---|
| 49 | else |
---|
| 50 | %ind_dot=regexp(SelectName,'\.\.\.'); |
---|
| 51 | % if ~isempty(ind_dot) |
---|
| 52 | % SelectName=SelectName(1:ind_dot-1); |
---|
| 53 | % end |
---|
| 54 | FullSelectName=fullfile(DirName,SelectName); |
---|
| 55 | end |
---|
| 56 | if exist(FullSelectName,'dir')% a directory has been selected |
---|
| 57 | % ListFiles=dir(FullSelectName); |
---|
| 58 | % ListDisplay=cell(numel(ListFiles),1); |
---|
| 59 | % for ilist=2:numel(ListDisplay)% suppress the first line '.' |
---|
| 60 | % ListDisplay{ilist-1}=ListFiles(ilist).name; |
---|
| 61 | % end |
---|
| 62 | % set(hObject,'Value',1) |
---|
| 63 | % set(hObject,'String',ListDisplay) |
---|
| 64 | % if strcmp(selectname,'..') |
---|
| 65 | % FullSelectName=fileparts(fileparts(FullSelectName)); |
---|
| 66 | % end |
---|
| 67 | ListFiles=list_files(FullSelectName); |
---|
| 68 | set(hObject,'Value',1) |
---|
| 69 | set(hObject,'String',ListFiles) |
---|
| 70 | set(hfig,'UserData',FullSelectName)% record the new dir name |
---|
| 71 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
| 72 | set(htitlebox,'String',FullSelectName) |
---|
| 73 | elseif exist(FullSelectName,'file')%visualise the field if it exists |
---|
| 74 | FileType=get_file_type(FullSelectName); |
---|
| 75 | if strcmp(FileType,'txt') |
---|
| 76 | edit(FullSelectName) |
---|
| 77 | elseif strcmp(FileType,'xml') |
---|
| 78 | editxml(FullSelectName) |
---|
| 79 | elseif strcmp(FileType,'figure') |
---|
| 80 | open(FullSelectName) |
---|
| 81 | else |
---|
| 82 | %uvmat(FullSelectName); |
---|
| 83 | switch option |
---|
| 84 | case 'file browser' |
---|
| 85 | hfig=get(hObject,'parent'); |
---|
| 86 | set(hfig,'UserData',FullSelectName); |
---|
| 87 | uiresume(hfig) |
---|
| 88 | case 'series status' |
---|
| 89 | uvmat(FullSelectName); |
---|
| 90 | end |
---|
| 91 | end |
---|
| 92 | set(gcbo,'Value',1) |
---|
| 93 | end |
---|
| 94 | |
---|
| 95 | %------------------------------------------------------------------------ |
---|
| 96 | % --- launched by selecting home |
---|
| 97 | function home_dir(hObject,event) |
---|
| 98 | DirName=pwd; |
---|
| 99 | |
---|
| 100 | ListFiles=list_files(DirName);% list the directory content |
---|
| 101 | hfig=get(hObject,'parent'); |
---|
| 102 | set(hfig,'UserData',DirName)% record the new dir name |
---|
| 103 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
| 104 | set(htitlebox,'String',DirName) |
---|
| 105 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
| 106 | set(hlist,'String',ListFiles) |
---|
| 107 | %------------------------------------------------------------------------ |
---|
| 108 | |
---|
| 109 | %------------------------------------------------------------------------ |
---|
| 110 | % --- launched by refreshing the display figure |
---|
| 111 | function refresh_GUI(hfig,event) |
---|
| 112 | %------------------------------------------------------------------------ |
---|
| 113 | DirName=get(hfig,'UserData'); |
---|
| 114 | ListFiles=list_files(DirName);% list the directory content |
---|
| 115 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
| 116 | set(hlist,'String',ListFiles) |
---|
| 117 | return |
---|
| 118 | |
---|
| 119 | %TODO adapt to series status |
---|
| 120 | hseries=findobj(allchild(0),'tag','series'); |
---|
| 121 | hstatus=findobj(hseries,'tag','status'); |
---|
| 122 | StatusData=get(hstatus,'UserData'); |
---|
| 123 | TimeStart=0; |
---|
| 124 | if isfield(StatusData,'TimeStart') |
---|
| 125 | TimeStart=StatusData.TimeStart; |
---|
| 126 | end |
---|
| 127 | % testrecent=0; |
---|
| 128 | % datnum=zeros(numel(ListDisplay),1); |
---|
| 129 | % for ilist=1:numel(ListDisplay) |
---|
| 130 | % ListDisplay{ilist}=ListFiles(ilist).name; |
---|
| 131 | % if ListFiles(ilist).isdir |
---|
| 132 | % ListDisplay{ilist}=['/' ListDisplay{ilist}]; |
---|
| 133 | % elseif isfield(ListFiles(ilist),'datenum') |
---|
| 134 | % datnum(ilist)=ListFiles(ilist).datenum;%only available in recent matlab versions |
---|
| 135 | % testrecent=1; |
---|
| 136 | % if datnum(ilist)<TimeStart |
---|
| 137 | % ListDisplay{ilist}=[ListDisplay{ilist} ' --OLD--']; |
---|
| 138 | % end |
---|
| 139 | % end |
---|
| 140 | % end |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | %% Look at date of creation |
---|
| 144 | ListDisplay=ListDisplay(datnum~=0); |
---|
| 145 | datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files |
---|
| 146 | |
---|
| 147 | NbOutputFile=[]; |
---|
| 148 | if isempty(datnum) |
---|
| 149 | if testrecent |
---|
| 150 | message='no result created yet'; |
---|
| 151 | else |
---|
| 152 | message=''; |
---|
| 153 | end |
---|
| 154 | else |
---|
| 155 | [first,indfirst]=min(datnum); |
---|
| 156 | [last,indlast]=max(datnum); |
---|
| 157 | NbOutputFile_str='?'; |
---|
| 158 | if isfield(StatusData,'NbOutputFile') |
---|
| 159 | NbOutputFile=StatusData.NbOutputFile; |
---|
| 160 | NbOutputFile_str=num2str(NbOutputFile); |
---|
| 161 | end |
---|
| 162 | message={[num2str(numel(datnum)) ' file(s) done over ' NbOutputFile_str] ;['oldest modification: ' ListDisplay{indfirst} ' : ' datestr(first)];... |
---|
| 163 | ['latest modification: ' ListDisplay{indlast} ' : ' datestr(last)]}; |
---|
| 164 | end |
---|
| 165 | set(htitlebox,'String', [DirName{1};message]) |
---|
| 166 | |
---|
| 167 | %% update the waitbar |
---|
| 168 | hwaitbar=findobj(hfig,'tag','waitbar'); |
---|
| 169 | if ~isempty(NbOutputFile) |
---|
| 170 | BarPosition=get(hwaitbar,'Position'); |
---|
| 171 | BarPosition(3)=0.9*numel(datnum)/NbOutputFile; |
---|
| 172 | set(hwaitbar,'Position',BarPosition) |
---|
| 173 | end |
---|
| 174 | |
---|
| 175 | %------------------------------------------------------------------------- |
---|
| 176 | % list the content of a directory |
---|
| 177 | function ListFiles=list_files(DirName) |
---|
| 178 | %------------------------------------------------------------------------- |
---|
| 179 | ListStruct=dir(DirName); |
---|
| 180 | if numel(ListStruct)<1 |
---|
| 181 | ListFiles={}; |
---|
| 182 | return |
---|
| 183 | end |
---|
| 184 | if strcmp(ListStruct(1).name,'.') |
---|
| 185 | ListStruct(1)=[];%removes the first line ='.' |
---|
| 186 | end |
---|
| 187 | ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray |
---|
| 188 | ListFiles=ListCells(1,:);%list of file names |
---|
| 189 | check_dir=cell2mat(ListCells(4,:));% check directories |
---|
| 190 | ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','/$0');% put '/' in front of dir name display |
---|
| 191 | [tild,index_sort]=sort(check_dir,2,'descend');% sort |
---|
| 192 | ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file |
---|
| 193 | cell_remove=regexp(ListFiles,'^(-|\.|/\.)');% remove strings beginning by '/.',';' or '-' |
---|
| 194 | check_remove=cellfun('isempty',cell_remove); |
---|
| 195 | ListFiles=[{'/..'} ListFiles(check_remove)]; |
---|
| 196 | |
---|
| 197 | %------------------------------------------------------------------------- |
---|
| 198 | % launched by deleting the status figure |
---|
| 199 | function stop_status(hObject, eventdata) |
---|
| 200 | %------------------------------------------------------------------------- |
---|
| 201 | hciv=findobj(allchild(0),'tag','series'); |
---|
| 202 | hhciv=guidata(hciv); |
---|
| 203 | set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ |
---|
| 204 | set(hhciv.status,'BackgroundColor',[0 1 0]) |
---|
| 205 | delete(gcbf) |
---|
| 206 | |
---|