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