[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); |
---|
| 47 | uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', @(src,event)view_file(option,src,event),'tag','list',... |
---|
| 48 | 'FontUnits','points','FontSize',12); |
---|
| 49 | uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','titlebox','Max',2,... |
---|
| 50 | 'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold'); |
---|
| 51 | uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.01 0.2 0.07],'Callback',@(src,event)close(option,src,event),... |
---|
| 52 | 'String','Close','FontWeight','bold','FontUnits','points','FontSize',12); |
---|
| 53 | uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.1 0.01 0.2 0.07],'Callback',@refresh_GUI,... |
---|
| 54 | 'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12); |
---|
[606] | 55 | %set(hrefresh,'UserData',StatusData) |
---|
[610] | 56 | if strcmp(option,'status_display') %put a run advancement display |
---|
[609] | 57 | set(hfig,'DeleteFcn',@stop_status) |
---|
[606] | 58 | uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.81 0.01 0.05],'BackgroundColor',[1 0 0],'tag','waitbar'); |
---|
| 59 | uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]); |
---|
[609] | 60 | else %put a title and additional pushbuttons |
---|
| 61 | uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.81 0.8 0.03],... |
---|
| 62 | 'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left'); |
---|
| 63 | uicontrol('Style','pushbutton','Tag','backward','Units','normalized','Position',[0.05 0.8 0.1 0.07],... |
---|
| 64 | 'String','<--','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@backward); |
---|
| 65 | uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.01 0.2 0.07],... |
---|
| 66 | 'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir); |
---|
[606] | 67 | end |
---|
| 68 | drawnow |
---|
| 69 | end |
---|
[610] | 70 | refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName)% refresh the list of content of the current dir |
---|
| 71 | if ~strcmp(option,'status_display') |
---|
[609] | 72 | uiwait(hfig) |
---|
[610] | 73 | fileinput=get(hfig,'UserData');% retrieve the input file selection |
---|
| 74 | delete(hfig) |
---|
[609] | 75 | end |
---|
| 76 | |
---|
[610] | 77 | |
---|
| 78 | |
---|
[606] | 79 | %------------------------------------------------------------------------ |
---|
[610] | 80 | % --- launched by refreshing the display figure |
---|
| 81 | function refresh_GUI(hObject,InputFileName) |
---|
| 82 | %------------------------------------------------------------------------ |
---|
| 83 | if ~exist('InputFileName','var') |
---|
| 84 | InputFileName=''; |
---|
| 85 | end |
---|
| 86 | hfig=get(hObject,'parent'); |
---|
| 87 | DirName=get(hfig,'UserData'); |
---|
| 88 | ListFiles=list_files(DirName);% list the directory content |
---|
| 89 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
| 90 | set(hlist,'String',ListFiles) |
---|
| 91 | Value=[]; |
---|
| 92 | if ~isempty(InputFileName) |
---|
| 93 | Value=find(strcmp(InputFileName,ListFiles)); |
---|
| 94 | end |
---|
| 95 | if isempty(Value) |
---|
| 96 | Value=1; |
---|
| 97 | end |
---|
| 98 | set(hlist,'Value',Value) |
---|
| 99 | |
---|
| 100 | if strcmp(get(hfig,'Tag'),'status_display') |
---|
| 101 | |
---|
| 102 | hseries=findobj(allchild(0),'tag','series'); |
---|
| 103 | hstatus=findobj(hseries,'tag','status_display'); |
---|
| 104 | StatusData=get(hstatus,'UserData'); |
---|
| 105 | TimeStart=0; |
---|
| 106 | if isfield(StatusData,'TimeStart') |
---|
| 107 | TimeStart=StatusData.TimeStart; |
---|
| 108 | end |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | htitlebox=findobj(hfig,'tag','titlebox'); |
---|
| 112 | hlist=findobj(hfig,'tag','list'); |
---|
| 113 | OutputDir=get(hfig,'UserData'); |
---|
| 114 | |
---|
| 115 | testrecent=0; |
---|
| 116 | datnum=zeros(numel(ListDisplay),1); |
---|
| 117 | for ilist=1:numel(ListDisplay) |
---|
| 118 | ListDisplay{ilist}=ListFiles(ilist).name; |
---|
| 119 | if ListFiles(ilist).isdir |
---|
| 120 | ListDisplay{ilist}=['/' ListDisplay{ilist}]; |
---|
| 121 | elseif isfield(ListFiles(ilist),'datenum') |
---|
| 122 | datnum(ilist)=ListFiles(ilist).datenum;%only available in recent matlab versions |
---|
| 123 | testrecent=1; |
---|
| 124 | if datnum(ilist)<TimeStart |
---|
| 125 | ListDisplay{ilist}=[ListDisplay{ilist} ' --OLD--']; |
---|
| 126 | end |
---|
| 127 | end |
---|
| 128 | end |
---|
| 129 | |
---|
| 130 | %% Look at date of creation |
---|
| 131 | ListDisplay=ListDisplay(datnum~=0); |
---|
| 132 | datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files |
---|
| 133 | |
---|
| 134 | NbOutputFile=[]; |
---|
| 135 | if isempty(datnum) |
---|
| 136 | if testrecent |
---|
| 137 | message='no result created yet'; |
---|
| 138 | else |
---|
| 139 | message=''; |
---|
| 140 | end |
---|
| 141 | else |
---|
| 142 | [first,indfirst]=min(datnum); |
---|
| 143 | [last,indlast]=max(datnum); |
---|
| 144 | NbOutputFile_str='?'; |
---|
| 145 | if isfield(StatusData,'NbOutputFile') |
---|
| 146 | NbOutputFile=StatusData.NbOutputFile; |
---|
| 147 | NbOutputFile_str=num2str(NbOutputFile); |
---|
| 148 | end |
---|
| 149 | message={[num2str(numel(datnum)) ' file(s) done over ' NbOutputFile_str] ;['oldest modification: ' ListDisplay{indfirst} ' : ' datestr(first)];... |
---|
| 150 | ['latest modification: ' ListDisplay{indlast} ' : ' datestr(last)]}; |
---|
| 151 | end |
---|
| 152 | set(htitlebox,'String', [DirName{1};message]) |
---|
| 153 | |
---|
| 154 | %% update the waitbar |
---|
| 155 | hwaitbar=findobj(hfig,'tag','waitbar'); |
---|
| 156 | if ~isempty(NbOutputFile) |
---|
| 157 | BarPosition=get(hwaitbar,'Position'); |
---|
| 158 | BarPosition(3)=0.9*numel(datnum)/NbOutputFile; |
---|
| 159 | set(hwaitbar,'Position',BarPosition) |
---|
| 160 | end |
---|
| 161 | end |
---|
| 162 | |
---|
| 163 | %------------------------------------------------------------------------ |
---|
[609] | 164 | % --- launched by selecting an item on the file list |
---|
[606] | 165 | function view_file(option,hObject,event) |
---|
| 166 | %------------------------------------------------------------------------ |
---|
| 167 | list=get(hObject,'String'); |
---|
| 168 | index=get(hObject,'Value'); |
---|
[609] | 169 | hfig=get(hObject,'parent');%handle of the fig |
---|
| 170 | DirName=get(hfig,'UserData'); |
---|
| 171 | SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir |
---|
| 172 | if strcmp(SelectName,'..')% the upward dir option has been selected |
---|
[606] | 173 | FullSelectName=fileparts(DirName); |
---|
| 174 | else |
---|
[609] | 175 | %ind_dot=regexp(SelectName,'\.\.\.'); |
---|
| 176 | % if ~isempty(ind_dot) |
---|
| 177 | % SelectName=SelectName(1:ind_dot-1); |
---|
| 178 | % end |
---|
| 179 | FullSelectName=fullfile(DirName,SelectName); |
---|
[606] | 180 | end |
---|
| 181 | if exist(FullSelectName,'dir')% a directory has been selected |
---|
| 182 | % ListFiles=dir(FullSelectName); |
---|
| 183 | % ListDisplay=cell(numel(ListFiles),1); |
---|
| 184 | % for ilist=2:numel(ListDisplay)% suppress the first line '.' |
---|
| 185 | % ListDisplay{ilist-1}=ListFiles(ilist).name; |
---|
| 186 | % end |
---|
| 187 | % set(hObject,'Value',1) |
---|
| 188 | % set(hObject,'String',ListDisplay) |
---|
| 189 | % if strcmp(selectname,'..') |
---|
| 190 | % FullSelectName=fileparts(fileparts(FullSelectName)); |
---|
| 191 | % end |
---|
[609] | 192 | hbackward=findobj(hfig,'Tag','backward'); |
---|
| 193 | set(hbackward,'UserData',DirName); %store the current dir for future backward action |
---|
[606] | 194 | ListFiles=list_files(FullSelectName); |
---|
| 195 | set(hObject,'Value',1) |
---|
| 196 | set(hObject,'String',ListFiles) |
---|
| 197 | set(hfig,'UserData',FullSelectName)% record the new dir name |
---|
| 198 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
| 199 | set(htitlebox,'String',FullSelectName) |
---|
| 200 | elseif exist(FullSelectName,'file')%visualise the field if it exists |
---|
| 201 | FileType=get_file_type(FullSelectName); |
---|
| 202 | if strcmp(FileType,'txt') |
---|
| 203 | edit(FullSelectName) |
---|
| 204 | elseif strcmp(FileType,'xml') |
---|
| 205 | editxml(FullSelectName) |
---|
| 206 | elseif strcmp(FileType,'figure') |
---|
| 207 | open(FullSelectName) |
---|
| 208 | else |
---|
| 209 | %uvmat(FullSelectName); |
---|
| 210 | switch option |
---|
[609] | 211 | case 'browser' |
---|
[606] | 212 | hfig=get(hObject,'parent'); |
---|
| 213 | set(hfig,'UserData',FullSelectName); |
---|
| 214 | uiresume(hfig) |
---|
[610] | 215 | case 'status_display' |
---|
[606] | 216 | uvmat(FullSelectName); |
---|
| 217 | end |
---|
[609] | 218 | set(gcbo,'Value',1) |
---|
[606] | 219 | end |
---|
| 220 | end |
---|
| 221 | |
---|
[610] | 222 | %------------------------------------------------------------------------- |
---|
| 223 | % list the content of a directory |
---|
| 224 | function ListFiles=list_files(DirName) |
---|
| 225 | %------------------------------------------------------------------------- |
---|
| 226 | ListStruct=dir(DirName);% get structure of the current directory |
---|
| 227 | if numel(ListStruct)<1 |
---|
| 228 | ListFiles={}; |
---|
| 229 | return |
---|
| 230 | end |
---|
| 231 | if strcmp(ListStruct(1).name,'.') |
---|
| 232 | ListStruct(1)=[];%removes the first line ='.' |
---|
| 233 | end |
---|
| 234 | ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray |
---|
| 235 | ListFiles=ListCells(1,:);%list of file names |
---|
| 236 | check_dir=cell2mat(ListCells(4,:));% check directories |
---|
| 237 | ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display |
---|
| 238 | [tild,index_sort]=sort(check_dir,2,'descend');% sort |
---|
| 239 | ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file |
---|
| 240 | cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . ) |
---|
| 241 | check_keep=cellfun('isempty', cell_remove); |
---|
| 242 | ListFiles=[{'+/..'} ListFiles(check_keep)]; |
---|
| 243 | |
---|
[606] | 244 | %------------------------------------------------------------------------ |
---|
| 245 | % --- launched by selecting home |
---|
| 246 | function home_dir(hObject,event) |
---|
[609] | 247 | %------------------------------------------------------------------------ |
---|
[606] | 248 | DirName=pwd; |
---|
| 249 | ListFiles=list_files(DirName);% list the directory content |
---|
| 250 | hfig=get(hObject,'parent'); |
---|
| 251 | set(hfig,'UserData',DirName)% record the new dir name |
---|
| 252 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
| 253 | set(htitlebox,'String',DirName) |
---|
| 254 | hlist=findobj(hfig,'tag','list');% find the list object |
---|
| 255 | set(hlist,'String',ListFiles) |
---|
| 256 | %------------------------------------------------------------------------ |
---|
| 257 | |
---|
| 258 | %------------------------------------------------------------------------ |
---|
[609] | 259 | % --- launched by pressing the backward (<--) button |
---|
| 260 | function backward(hObject,event) |
---|
| 261 | %------------------------------------------------------------------------ |
---|
| 262 | PrevDir=get(hObject,'UserData'); |
---|
| 263 | if ~isempty(PrevDir) |
---|
| 264 | hfig=get(hObject,'parent'); |
---|
| 265 | set(hfig,'UserData',PrevDir) |
---|
| 266 | htitlebox=findobj(hfig,'tag','titlebox'); % display the new dir name |
---|
| 267 | set(htitlebox,'String',PrevDir) |
---|
| 268 | refresh_GUI(findobj(hfig,'Tag','refresh')) |
---|
| 269 | end |
---|
| 270 | |
---|
[606] | 271 | %------------------------------------------------------------------------- |
---|
[609] | 272 | % launched by deleting the status figure (only used in mode series status') |
---|
| 273 | function close(option,hObject, eventdata) |
---|
[606] | 274 | %------------------------------------------------------------------------- |
---|
[610] | 275 | if strcmp(option,'status_display') |
---|
[609] | 276 | hseries=findobj(allchild(0),'tag','series'); |
---|
[610] | 277 | hstatus=findobj(hfig,'Tag','status_display'); |
---|
[609] | 278 | set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ |
---|
| 279 | set(hhciv.status,'BackgroundColor',[0 1 0]) |
---|
| 280 | end |
---|
[606] | 281 | delete(gcbf) |
---|
| 282 | |
---|