[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] | 12 | function fileinput=uigetfile_uvmat(title,InputName) |
---|
| 13 | fileinput=''; %default file selection |
---|
[610] | 14 | if strcmp(title,'status_display') |
---|
| 15 | option='status_display'; |
---|
[609] | 16 | else |
---|
| 17 | option='browser'; |
---|
[606] | 18 | end |
---|
[615] | 19 | InputDir=pwd;%look in the current work directory if the input file does not exist |
---|
| 20 | InputFileName='';%default |
---|
| 21 | if 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] | 38 | end |
---|
| 39 | hfig=findobj(allchild(0),'tag',option); |
---|
[606] | 40 | if 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 |
---|
| 80 | end |
---|
[610] | 81 | refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName)% refresh the list of content of the current dir |
---|
| 82 | if ~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] | 89 | end |
---|
| 90 | |
---|
[615] | 91 | %------------------------------------------------------------------------ |
---|
| 92 | % --- launched by refreshing the display figure |
---|
| 93 | function titlebox_Callback(hObject,event) |
---|
| 94 | refresh_GUI(hObject) |
---|
| 95 | %------------------------------------------------------------------------ |
---|
[610] | 96 | |
---|
[606] | 97 | %------------------------------------------------------------------------ |
---|
[610] | 98 | % --- launched by refreshing the display figure |
---|
| 99 | function refresh_GUI(hObject,InputFileName) |
---|
| 100 | %------------------------------------------------------------------------ |
---|
| 101 | if ~exist('InputFileName','var') |
---|
| 102 | InputFileName=''; |
---|
| 103 | end |
---|
| 104 | hfig=get(hObject,'parent'); |
---|
[613] | 105 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
| 106 | set(hlist,'BackgroundColor',[1 1 0]) |
---|
| 107 | drawnow |
---|
[611] | 108 | htitlebox=findobj(hfig,'tag','titlebox'); |
---|
| 109 | DirName=get(htitlebox,'String'); |
---|
[612] | 110 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
| 111 | sort_option='name'; |
---|
| 112 | if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2) |
---|
| 113 | sort_option='date'; |
---|
| 114 | end |
---|
| 115 | hcheck_date=findobj(hfig,'tag','check_date'); |
---|
| 116 | ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content |
---|
[613] | 117 | |
---|
[610] | 118 | set(hlist,'String',ListFiles) |
---|
| 119 | Value=[]; |
---|
| 120 | if ~isempty(InputFileName) |
---|
| 121 | Value=find(strcmp(InputFileName,ListFiles)); |
---|
| 122 | end |
---|
| 123 | if isempty(Value) |
---|
| 124 | Value=1; |
---|
| 125 | end |
---|
| 126 | set(hlist,'Value',Value) |
---|
[613] | 127 | set(hlist,'BackgroundColor',[0.7 0.7 0.7]) |
---|
[612] | 128 | if 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 |
---|
| 183 | end |
---|
| 184 | |
---|
| 185 | %------------------------------------------------------------------------ |
---|
[609] | 186 | % --- launched by selecting an item on the file list |
---|
[612] | 187 | function dates_Callback(hObject,event) |
---|
[606] | 188 | %------------------------------------------------------------------------ |
---|
[612] | 189 | hfig=get(hObject,'parent'); |
---|
| 190 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
| 191 | if get(hObject,'Value') |
---|
| 192 | set(hsort_option,'Visible','on') |
---|
| 193 | set(hsort_option,'Value',2) |
---|
| 194 | else |
---|
| 195 | set(hsort_option,'Visible','off') |
---|
| 196 | end |
---|
| 197 | refresh_GUI(hObject,[]) |
---|
| 198 | |
---|
| 199 | %------------------------------------------------------------------------ |
---|
| 200 | % --- launched by selecting an item on the file list |
---|
| 201 | function list_Callback(option,hObject,event) |
---|
| 202 | %------------------------------------------------------------------------ |
---|
[617] | 203 | hfig=get(hObject,'parent');%handle of the fig |
---|
| 204 | % if ~strcmp(get(hfig,'SelectionType'),'open') |
---|
| 205 | % return %select double click |
---|
| 206 | % end |
---|
| 207 | set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action |
---|
[615] | 208 | drawnow |
---|
[606] | 209 | list=get(hObject,'String'); |
---|
| 210 | index=get(hObject,'Value'); |
---|
[617] | 211 | |
---|
[611] | 212 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
| 213 | DirName=get(htitlebox,'String'); |
---|
[609] | 214 | SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir |
---|
[612] | 215 | ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond '...' |
---|
| 216 | if ~isempty(ind_dot) |
---|
| 217 | SelectName=SelectName(1:ind_dot-1); |
---|
| 218 | end |
---|
[609] | 219 | if strcmp(SelectName,'..')% the upward dir option has been selected |
---|
[606] | 220 | FullSelectName=fileparts(DirName); |
---|
| 221 | else |
---|
[609] | 222 | FullSelectName=fullfile(DirName,SelectName); |
---|
[606] | 223 | end |
---|
| 224 | if 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] | 251 | elseif 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 |
---|
| 269 | end |
---|
[615] | 270 | set(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] | 274 | function ListFiles=list_files(DirName,check_date,sort_option) |
---|
[610] | 275 | %------------------------------------------------------------------------- |
---|
| 276 | ListStruct=dir(DirName);% get structure of the current directory |
---|
[611] | 277 | if numel(ListStruct)<1 % case of empty dir |
---|
[610] | 278 | ListFiles={}; |
---|
| 279 | return |
---|
| 280 | end |
---|
| 281 | ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray |
---|
| 282 | ListFiles=ListCells(1,:);%list of file names |
---|
[611] | 283 | check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files |
---|
[610] | 284 | ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display |
---|
[612] | 285 | if 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 |
---|
| 289 | else |
---|
| 290 | [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list |
---|
| 291 | end |
---|
| 292 | ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file |
---|
[610] | 293 | cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . ) |
---|
[612] | 294 | check_keep=cellfun('isempty', cell_remove); |
---|
[610] | 295 | ListFiles=[{'+/..'} ListFiles(check_keep)]; |
---|
[612] | 296 | if check_date |
---|
| 297 | ListDateString=ListCells(2,:);%list of file dates |
---|
| 298 | ListDateString(check_dir)={''}; |
---|
| 299 | ListDateString=ListDateString(index_sort);% sort the corresponding dates |
---|
| 300 | ListDateString=[{''} ListDateString(check_keep)]; |
---|
| 301 | ListFiles=[ListFiles; ListDateString]; |
---|
| 302 | ListFiles=cell2tab(ListFiles','...'); |
---|
| 303 | end |
---|
[610] | 304 | |
---|
[606] | 305 | %------------------------------------------------------------------------ |
---|
| 306 | % --- launched by selecting home |
---|
| 307 | function home_dir(hObject,event) |
---|
[609] | 308 | %------------------------------------------------------------------------ |
---|
[606] | 309 | DirName=pwd; |
---|
| 310 | hfig=get(hObject,'parent'); |
---|
[613] | 311 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
| 312 | set(hlist,'BackgroundColor',[1 1 0]) |
---|
| 313 | drawnow |
---|
[612] | 314 | sort_option='name';%default |
---|
| 315 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
| 316 | if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2) |
---|
| 317 | sort_option='date'; |
---|
| 318 | end |
---|
| 319 | hcheck_date=findobj(hfig,'tag','check_date'); |
---|
| 320 | ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content |
---|
[611] | 321 | htitlebox=findobj(hfig,'Tag','titlebox'); |
---|
| 322 | set(htitlebox,'String',DirName)% record the new dir name |
---|
[613] | 323 | set(hlist,'Value',1) |
---|
[606] | 324 | set(hlist,'String',ListFiles) |
---|
[613] | 325 | set(hlist,'BackgroundColor',[0.7 0.7 0.7]) |
---|
[606] | 326 | %------------------------------------------------------------------------ |
---|
| 327 | |
---|
| 328 | %------------------------------------------------------------------------ |
---|
[609] | 329 | % --- launched by pressing the backward (<--) button |
---|
| 330 | function backward(hObject,event) |
---|
| 331 | %------------------------------------------------------------------------ |
---|
| 332 | PrevDir=get(hObject,'UserData'); |
---|
| 333 | if ~isempty(PrevDir) |
---|
| 334 | hfig=get(hObject,'parent'); |
---|
| 335 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
| 336 | set(htitlebox,'String',PrevDir) |
---|
| 337 | refresh_GUI(findobj(hfig,'Tag','refresh')) |
---|
| 338 | end |
---|
| 339 | |
---|
[606] | 340 | %------------------------------------------------------------------------- |
---|
[609] | 341 | % launched by deleting the status figure (only used in mode series status') |
---|
| 342 | function close(option,hObject, eventdata) |
---|
[606] | 343 | %------------------------------------------------------------------------- |
---|
[610] | 344 | if 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]) |
---|
| 349 | end |
---|
[606] | 350 | delete(gcbf) |
---|
| 351 | |
---|