[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 |
---|
[635] | 69 | set(hfig,'DeleteFcn',@(src,event)close(option,src,event)) |
---|
| 70 | uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.85 0.9 0.04]); |
---|
| 71 | uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.85 0.01 0.04],'BackgroundColor',[1 0 0],'tag','waitbar'); |
---|
| 72 | |
---|
[609] | 73 | else %put a title and additional pushbuttons |
---|
[612] | 74 | uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.75 0.6 0.03],'BackgroundColor',BackgroundColor,... |
---|
[609] | 75 | 'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left'); |
---|
[612] | 76 | |
---|
[611] | 77 | uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.005 0.2 0.07],... |
---|
[609] | 78 | 'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir); |
---|
[606] | 79 | end |
---|
| 80 | drawnow |
---|
| 81 | end |
---|
[610] | 82 | refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName)% refresh the list of content of the current dir |
---|
| 83 | if ~strcmp(option,'status_display') |
---|
[609] | 84 | uiwait(hfig) |
---|
[620] | 85 | if ishandle(hfig) |
---|
[611] | 86 | htitlebox=findobj(hfig,'Tag','titlebox'); |
---|
| 87 | fileinput=get(htitlebox,'String');% retrieve the input file selection |
---|
[610] | 88 | delete(hfig) |
---|
[620] | 89 | end |
---|
[609] | 90 | end |
---|
| 91 | |
---|
[615] | 92 | %------------------------------------------------------------------------ |
---|
| 93 | % --- launched by refreshing the display figure |
---|
| 94 | function titlebox_Callback(hObject,event) |
---|
| 95 | refresh_GUI(hObject) |
---|
| 96 | %------------------------------------------------------------------------ |
---|
[610] | 97 | |
---|
[606] | 98 | %------------------------------------------------------------------------ |
---|
[610] | 99 | % --- launched by refreshing the display figure |
---|
| 100 | function refresh_GUI(hObject,InputFileName) |
---|
| 101 | %------------------------------------------------------------------------ |
---|
| 102 | if ~exist('InputFileName','var') |
---|
| 103 | InputFileName=''; |
---|
| 104 | end |
---|
| 105 | hfig=get(hObject,'parent'); |
---|
[613] | 106 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
| 107 | set(hlist,'BackgroundColor',[1 1 0]) |
---|
| 108 | drawnow |
---|
[611] | 109 | htitlebox=findobj(hfig,'tag','titlebox'); |
---|
| 110 | DirName=get(htitlebox,'String'); |
---|
[612] | 111 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
[635] | 112 | if strcmp(get(hfig,'Tag'),'status_display') |
---|
[610] | 113 | hseries=findobj(allchild(0),'tag','series'); |
---|
[635] | 114 | hstatus=findobj(hseries,'tag','status'); |
---|
[610] | 115 | StatusData=get(hstatus,'UserData'); |
---|
| 116 | TimeStart=0; |
---|
| 117 | if isfield(StatusData,'TimeStart') |
---|
| 118 | TimeStart=StatusData.TimeStart; |
---|
[635] | 119 | end |
---|
[610] | 120 | hlist=findobj(hfig,'tag','list'); |
---|
[635] | 121 | testrecent=0; |
---|
[610] | 122 | NbOutputFile=[]; |
---|
[635] | 123 | if isfield(StatusData,'NbOutputFile') |
---|
| 124 | NbOutputFile=StatusData.NbOutputFile; |
---|
| 125 | NbOutputFile_str=num2str(NbOutputFile); |
---|
[610] | 126 | end |
---|
[635] | 127 | [ListFiles,NumFiles]=list_files(DirName,1,TimeStart);% list the directory content |
---|
[610] | 128 | |
---|
| 129 | %% update the waitbar |
---|
| 130 | hwaitbar=findobj(hfig,'tag','waitbar'); |
---|
| 131 | if ~isempty(NbOutputFile) |
---|
| 132 | BarPosition=get(hwaitbar,'Position'); |
---|
[635] | 133 | BarPosition(3)=0.9*max(0.01,NumFiles/NbOutputFile);% the bar width cannot be set to 0, set to 0.01 instead |
---|
[610] | 134 | set(hwaitbar,'Position',BarPosition) |
---|
| 135 | end |
---|
[635] | 136 | else |
---|
| 137 | sort_option='name'; |
---|
| 138 | if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2) |
---|
| 139 | sort_option='date'; |
---|
| 140 | end |
---|
| 141 | hcheck_date=findobj(hfig,'tag','check_date'); |
---|
| 142 | [ListFiles,NumFiles]=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content |
---|
[610] | 143 | end |
---|
| 144 | |
---|
[635] | 145 | set(hlist,'String',ListFiles) |
---|
| 146 | Value=[]; |
---|
| 147 | if ~isempty(InputFileName) |
---|
| 148 | Value=find(strcmp(InputFileName,ListFiles)); |
---|
| 149 | end |
---|
| 150 | if isempty(Value) |
---|
| 151 | Value=1; |
---|
| 152 | end |
---|
| 153 | set(hlist,'Value',Value) |
---|
| 154 | set(hlist,'BackgroundColor',[0.7 0.7 0.7]) |
---|
[610] | 155 | %------------------------------------------------------------------------ |
---|
[609] | 156 | % --- launched by selecting an item on the file list |
---|
[612] | 157 | function dates_Callback(hObject,event) |
---|
[606] | 158 | %------------------------------------------------------------------------ |
---|
[612] | 159 | hfig=get(hObject,'parent'); |
---|
| 160 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
| 161 | if get(hObject,'Value') |
---|
| 162 | set(hsort_option,'Visible','on') |
---|
| 163 | set(hsort_option,'Value',2) |
---|
| 164 | else |
---|
| 165 | set(hsort_option,'Visible','off') |
---|
| 166 | end |
---|
| 167 | refresh_GUI(hObject,[]) |
---|
| 168 | |
---|
| 169 | %------------------------------------------------------------------------ |
---|
| 170 | % --- launched by selecting an item on the file list |
---|
| 171 | function list_Callback(option,hObject,event) |
---|
| 172 | %------------------------------------------------------------------------ |
---|
[617] | 173 | hfig=get(hObject,'parent');%handle of the fig |
---|
| 174 | % if ~strcmp(get(hfig,'SelectionType'),'open') |
---|
| 175 | % return %select double click |
---|
| 176 | % end |
---|
| 177 | set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action |
---|
[615] | 178 | drawnow |
---|
[606] | 179 | list=get(hObject,'String'); |
---|
| 180 | index=get(hObject,'Value'); |
---|
[617] | 181 | |
---|
[611] | 182 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
| 183 | DirName=get(htitlebox,'String'); |
---|
[609] | 184 | SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir |
---|
[612] | 185 | ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond '...' |
---|
| 186 | if ~isempty(ind_dot) |
---|
| 187 | SelectName=SelectName(1:ind_dot-1); |
---|
| 188 | end |
---|
[609] | 189 | if strcmp(SelectName,'..')% the upward dir option has been selected |
---|
[606] | 190 | FullSelectName=fileparts(DirName); |
---|
| 191 | else |
---|
[609] | 192 | FullSelectName=fullfile(DirName,SelectName); |
---|
[606] | 193 | end |
---|
| 194 | if exist(FullSelectName,'dir')% a directory has been selected |
---|
[612] | 195 | % ListFiles=dir(FullSelectName); |
---|
| 196 | % ListDisplay=cell(numel(ListFiles),1); |
---|
| 197 | % for ilist=2:numel(ListDisplay)% suppress the first line '.' |
---|
| 198 | % ListDisplay{ilist-1}=ListFiles(ilist).name; |
---|
| 199 | % end |
---|
| 200 | % set(hObject,'Value',1) |
---|
| 201 | % set(hObject,'String',ListDisplay) |
---|
| 202 | % if strcmp(selectname,'..') |
---|
| 203 | % FullSelectName=fileparts(fileparts(FullSelectName)); |
---|
| 204 | % end |
---|
[613] | 205 | set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action |
---|
| 206 | drawnow |
---|
[609] | 207 | hbackward=findobj(hfig,'Tag','backward'); |
---|
| 208 | set(hbackward,'UserData',DirName); %store the current dir for future backward action |
---|
[612] | 209 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
| 210 | sort_option='name';%default |
---|
| 211 | if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2) |
---|
| 212 | sort_option='date'; |
---|
| 213 | end |
---|
| 214 | hcheck_date=findobj(hfig,'tag','check_date'); |
---|
[613] | 215 | |
---|
[612] | 216 | ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option);% list the directory content |
---|
[606] | 217 | set(hObject,'Value',1) |
---|
| 218 | set(hObject,'String',ListFiles) |
---|
[613] | 219 | set(hObject,'BackgroundColor',[0.7 0.7 0.7]) |
---|
[611] | 220 | set(htitlebox,'String',FullSelectName)% record the new dir name |
---|
[606] | 221 | elseif exist(FullSelectName,'file')%visualise the field if it exists |
---|
| 222 | FileType=get_file_type(FullSelectName); |
---|
| 223 | if strcmp(FileType,'txt') |
---|
| 224 | edit(FullSelectName) |
---|
| 225 | elseif strcmp(FileType,'xml') |
---|
| 226 | editxml(FullSelectName) |
---|
[612] | 227 | elseif strcmp(FileType,'figure') |
---|
[606] | 228 | open(FullSelectName) |
---|
| 229 | else |
---|
| 230 | %uvmat(FullSelectName); |
---|
| 231 | switch option |
---|
[609] | 232 | case 'browser' |
---|
[612] | 233 | set(htitlebox,'String',FullSelectName); |
---|
| 234 | uiresume(hfig) |
---|
[610] | 235 | case 'status_display' |
---|
[612] | 236 | uvmat(FullSelectName); |
---|
[606] | 237 | end |
---|
| 238 | end |
---|
| 239 | end |
---|
[615] | 240 | set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end |
---|
[606] | 241 | |
---|
[610] | 242 | %------------------------------------------------------------------------- |
---|
| 243 | % list the content of a directory |
---|
[635] | 244 | function [ListFiles,NumFiles]=list_files(DirName,check_date,sort_option) |
---|
[610] | 245 | %------------------------------------------------------------------------- |
---|
| 246 | ListStruct=dir(DirName);% get structure of the current directory |
---|
[635] | 247 | NumFiles=0; %default |
---|
[611] | 248 | if numel(ListStruct)<1 % case of empty dir |
---|
[610] | 249 | ListFiles={}; |
---|
| 250 | return |
---|
| 251 | end |
---|
| 252 | ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray |
---|
| 253 | ListFiles=ListCells(1,:);%list of file names |
---|
[611] | 254 | check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files |
---|
[610] | 255 | ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display |
---|
[635] | 256 | ListDates=cell2mat(ListCells(5,:));%list of numerical dates |
---|
| 257 | if isnumeric(sort_option) |
---|
| 258 | check_old=ListDates<sort_option-1;% -1 is put to account for a 1 s delay in the record of starting time |
---|
| 259 | NumFiles=numel(find(~check_old&~check_dir)); |
---|
| 260 | end |
---|
| 261 | if ~isempty(find(~check_dir)) |
---|
| 262 | ListDates(check_dir)=max(ListDates(~check_dir))+1000; % we set the dir in front |
---|
| 263 | end |
---|
| 264 | if isnumeric(sort_option)|| strcmp(sort_option,'date') |
---|
[612] | 265 | [tild,index_sort]=sort(ListDates,2,'descend');% sort files by chronological order, recent first, put the dir first in the list |
---|
| 266 | else |
---|
| 267 | [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list |
---|
| 268 | end |
---|
| 269 | ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file |
---|
[610] | 270 | cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . ) |
---|
[612] | 271 | check_keep=cellfun('isempty', cell_remove); |
---|
[610] | 272 | ListFiles=[{'+/..'} ListFiles(check_keep)]; |
---|
[612] | 273 | if check_date |
---|
[635] | 274 | ListDateString=ListCells(2,:);%list of file dates |
---|
| 275 | if isnumeric(sort_option) |
---|
| 276 | ListDateString(check_old)={'--OLD--'}; |
---|
| 277 | end |
---|
| 278 | ListDateString(check_dir)={''}; |
---|
| 279 | ListDateString=ListDateString(index_sort);% sort the corresponding dates |
---|
| 280 | ListDateString=[{''} ListDateString(check_keep)]; |
---|
| 281 | ListFiles=[ListFiles; ListDateString]; |
---|
| 282 | ListFiles=cell2tab(ListFiles','...'); |
---|
[612] | 283 | end |
---|
[610] | 284 | |
---|
[606] | 285 | %------------------------------------------------------------------------ |
---|
| 286 | % --- launched by selecting home |
---|
| 287 | function home_dir(hObject,event) |
---|
[609] | 288 | %------------------------------------------------------------------------ |
---|
[606] | 289 | DirName=pwd; |
---|
| 290 | hfig=get(hObject,'parent'); |
---|
[613] | 291 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
| 292 | set(hlist,'BackgroundColor',[1 1 0]) |
---|
| 293 | drawnow |
---|
[612] | 294 | sort_option='name';%default |
---|
| 295 | hsort_option=findobj(hfig,'tag','sort_option'); |
---|
| 296 | if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2) |
---|
| 297 | sort_option='date'; |
---|
| 298 | end |
---|
| 299 | hcheck_date=findobj(hfig,'tag','check_date'); |
---|
| 300 | ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content |
---|
[611] | 301 | htitlebox=findobj(hfig,'Tag','titlebox'); |
---|
| 302 | set(htitlebox,'String',DirName)% record the new dir name |
---|
[613] | 303 | set(hlist,'Value',1) |
---|
[606] | 304 | set(hlist,'String',ListFiles) |
---|
[613] | 305 | set(hlist,'BackgroundColor',[0.7 0.7 0.7]) |
---|
[606] | 306 | %------------------------------------------------------------------------ |
---|
| 307 | |
---|
| 308 | %------------------------------------------------------------------------ |
---|
[609] | 309 | % --- launched by pressing the backward (<--) button |
---|
| 310 | function backward(hObject,event) |
---|
| 311 | %------------------------------------------------------------------------ |
---|
| 312 | PrevDir=get(hObject,'UserData'); |
---|
| 313 | if ~isempty(PrevDir) |
---|
| 314 | hfig=get(hObject,'parent'); |
---|
| 315 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
| 316 | set(htitlebox,'String',PrevDir) |
---|
| 317 | refresh_GUI(findobj(hfig,'Tag','refresh')) |
---|
| 318 | end |
---|
| 319 | |
---|
[606] | 320 | %------------------------------------------------------------------------- |
---|
[609] | 321 | % launched by deleting the status figure (only used in mode series status') |
---|
| 322 | function close(option,hObject, eventdata) |
---|
[606] | 323 | %------------------------------------------------------------------------- |
---|
[610] | 324 | if strcmp(option,'status_display') |
---|
[635] | 325 | hseries=findobj(allchild(0),'Tag','series'); |
---|
| 326 | hstatus=findobj(hseries,'Tag','status'); |
---|
| 327 | set(hstatus,'value',0) %reset the status uicontrol in the GUI series |
---|
| 328 | set(hstatus,'BackgroundColor',[0 1 0]) |
---|
[609] | 329 | end |
---|
[606] | 330 | delete(gcbf) |
---|
| 331 | |
---|