rri_select_file.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. function [selected_file, selected_path] = rri_select_file(varargin)
  2. %
  3. % USAGE: [selected_file, selected_path] = ...
  4. % rri_select_file(dir_name, fig_title)
  5. %
  6. % Allow user to select a file from a list of Matlab competible
  7. % file format
  8. %
  9. % Example:
  10. %
  11. % [selected_file, selected_path] = ...
  12. % rri_select_file('/usr','Select Data File');
  13. %
  14. % See Also RRI_GETFILES
  15. % -- Created June 2001 by Wilkin Chau, Rotman Research Institute
  16. %
  17. % use rri_select_file to open & save Matlab recognized format
  18. % -- Modified Dec 2002 by Jimmy Shen, Rotman Research Institute
  19. %
  20. if nargin == 0 | ischar(varargin{1}) % create rri_select_file figure
  21. dir_name = '';
  22. fig_title = 'Select a File';
  23. if nargin > 0
  24. dir_name = varargin{1};
  25. end
  26. if nargin > 1
  27. fig_title = varargin{2};
  28. end
  29. Init(fig_title,dir_name);
  30. uiwait; % wait for user finish
  31. selected_path = getappdata(gcf,'SelectedDirectory');
  32. selected_file = getappdata(gcf,'SelectedFile');
  33. cd (getappdata(gcf,'StartDirectory'));
  34. close(gcf);
  35. return;
  36. end;
  37. % clear the message line,
  38. %
  39. h = findobj(gcf,'Tag','MessageLine');
  40. set(h,'String','');
  41. action = varargin{1}{1};
  42. % change 'File format':
  43. % update 'Files' & 'File selection' based on file pattern
  44. %
  45. if strcmp(action,'EditFilter'),
  46. EditFilter;
  47. % run delete_fig when figure is closing
  48. %
  49. elseif strcmp(action,'delete_fig'),
  50. delete_fig;
  51. % select 'Directories':
  52. % go into the selected dir
  53. % update 'Files' & 'File selection' based on file pattern
  54. %
  55. elseif strcmp(action,'select_dir'),
  56. select_dir;
  57. % select 'Files':
  58. % update 'File selection'
  59. %
  60. elseif strcmp(action,'select_file'),
  61. select_file;
  62. % change 'File selection':
  63. % if it is a file, select that,
  64. % if it is more than a file (*), select those,
  65. % if it is a directory, select based on file pattern
  66. %
  67. elseif strcmp(action,'EditSelection'),
  68. EditSelection;
  69. % clicked 'Select'
  70. %
  71. elseif strcmp(action,'DONE_BUTTON_PRESSED'),
  72. h = findobj(gcf,'Tag','SelectionEdit');
  73. [filepath,filename,fileext] = fileparts(get(h,'String'));
  74. if isempty(filepath) | isempty(filename) | isempty(fileext)
  75. setappdata(gcf,'SelectedDirectory',[]);
  76. setappdata(gcf,'SelectedFile',[]);
  77. else
  78. if ~strcmp(filepath(end),filesep) % not end with filesep
  79. filepath = [filepath filesep]; % add a filesep to filepath
  80. end
  81. setappdata(gcf,'SelectedDirectory',filepath);
  82. setappdata(gcf,'SelectedFile',[filename fileext]);
  83. end
  84. if getappdata(gcf,'ready') % ready to exit
  85. uiresume;
  86. end
  87. % clicked 'cancel'
  88. %
  89. elseif strcmp(action,'CANCEL_BUTTON_PRESSED'),
  90. setappdata(gcf,'SelectedDirectory',[]);
  91. setappdata(gcf,'SelectedFile',[]);
  92. set(findobj(gcf,'Tag','FileList'),'String','');
  93. uiresume;
  94. end;
  95. return;
  96. % --------------------------------------------------------------------
  97. function Init(fig_title,dir_name),
  98. StartDirectory = pwd;
  99. if isempty(StartDirectory),
  100. StartDirectory = filesep;
  101. end;
  102. filter_disp = {'JPEG image (*.jpg)', ...
  103. 'TIFF image, compressed (*.tif)', ...
  104. 'EPS Level 1 (*.eps)', ...
  105. 'Adobe Illustrator 88 (*.ai)', ...
  106. 'Enhanced metafile (*.emf)', ...
  107. 'Matlab Figure (*.fig)', ...
  108. 'Matlab M-file (*.m)', ...
  109. 'Portable bitmap (*.pbm)', ...
  110. 'Paintbrush 24-bit (*.pcx)', ...
  111. 'Portable Graymap (*.pgm)', ...
  112. 'Portable Network Graphics (*.png)', ...
  113. 'Portable Pixmap (*.ppm)', ...
  114. };
  115. filter_string = {'*.jpg', ...
  116. '*.tif', ...
  117. '*.eps', ...
  118. '*.ai', ...
  119. '*.emf', ...
  120. '*.fig', ...
  121. '*.m', ...
  122. '*.pbm', ...
  123. '*.pcx', ...
  124. '*.pgm', ...
  125. '*.png', ...
  126. '*.ppm', ...
  127. };
  128. % filter_disp = char(filter_disp);
  129. filter_string = char(filter_string);
  130. margine = 0.05;
  131. line_height = 0.07;
  132. char_height = line_height*0.8;
  133. save_setting_status = 'on';
  134. rri_select_file_pos = [];
  135. try
  136. load('pls_profile');
  137. catch
  138. end
  139. if ~isempty(rri_select_file_pos) & strcmp(save_setting_status,'on')
  140. pos = rri_select_file_pos;
  141. else
  142. w = 0.4;
  143. h = 0.6;
  144. x = (1-w)/2;
  145. y = (1-h)/2;
  146. pos = [x y w h];
  147. end
  148. h0 = figure('parent',0, 'Color',[0.8 0.8 0.8], ...
  149. 'Units','normal', ...
  150. 'Name',fig_title, ...
  151. 'NumberTitle','off', ...
  152. 'MenuBar','none', ...
  153. 'Position', pos, ...
  154. 'deleteFcn','rri_select_file({''delete_fig''});', ...
  155. 'WindowStyle', 'modal', ...
  156. 'Tag','GetFilesFigure', ...
  157. 'ToolBar','none');
  158. x = margine;
  159. y = 1 - 1*line_height - margine;
  160. w = 1-2*x;
  161. h = char_height;
  162. pos = [x y w h];
  163. h1 = uicontrol('Parent',h0, ... % Filter Label
  164. 'Style','text', ...
  165. 'Units','normal', ...
  166. 'BackgroundColor',[0.8 0.8 0.8], ...
  167. 'fontunit','normal', ...
  168. 'FontSize',0.5, ...
  169. 'HorizontalAlignment','left', ...
  170. 'Position', pos, ...
  171. 'String','Choose one of the file format:', ...
  172. 'Tag','FilterLabel');
  173. y = 1 - 2*line_height - margine + line_height*0.2;
  174. w = 1-2*x;
  175. pos = [x y w h];
  176. h_filter = uicontrol('Parent',h0, ... % Filter list
  177. 'Style','popupmenu', ...
  178. 'Units','normal', ...
  179. 'BackgroundColor',[1 1 1], ...
  180. 'fontunit','normal', ...
  181. 'FontSize',0.5, ...
  182. 'HorizontalAlignment','left', ...
  183. 'Position', pos, ...
  184. 'String', filter_disp, ...
  185. 'user', filter_string, ...
  186. 'value', 1, ...
  187. 'Callback','rri_select_file({''EditFilter''});', ...
  188. 'Tag','FilterEdit');
  189. y = 1 - 3*line_height - margine;
  190. w = 0.5 - x - margine/2;
  191. pos = [x y w h];
  192. h1 = uicontrol('Parent',h0, ... % Directory Label
  193. 'Style','text', ...
  194. 'Units','normal', ...
  195. 'BackgroundColor',[0.8 0.8 0.8], ...
  196. 'fontunit','normal', ...
  197. 'FontSize',0.5, ...
  198. 'HorizontalAlignment','left', ...
  199. 'ListboxTop',0, ...
  200. 'Position', pos, ...
  201. 'String','Directories', ...
  202. 'Tag','DirectoryLabel');
  203. x = 0.5;
  204. y = 1 - 3*line_height - margine;
  205. w = 0.5 - margine;
  206. pos = [x y w h];
  207. h1 = uicontrol('Parent',h0, ... % File Label
  208. 'Style','text', ...
  209. 'Units','normal', ...
  210. 'BackgroundColor',[0.8 0.8 0.8], ...
  211. 'fontunit','normal', ...
  212. 'FontSize',0.5, ...
  213. 'HorizontalAlignment','left', ...
  214. 'ListboxTop',0, ...
  215. 'Position', pos, ...
  216. 'String','Files', ...
  217. 'Tag','FileLabel');
  218. x = margine;
  219. y = 4*line_height + margine;
  220. w = 0.5 - x - margine/2;
  221. h = 1 - 7*line_height - 2*margine;
  222. pos = [x y w h];
  223. h_dir = uicontrol('Parent',h0, ... % Directory Listbox
  224. 'Style','listbox', ...
  225. 'Units','normal', ...
  226. 'fontunit','normal', ...
  227. 'FontSize',0.08, ...
  228. 'HorizontalAlignment','left', ...
  229. 'Interruptible', 'off', ...
  230. 'ListboxTop',1, ...
  231. 'Position', pos, ...
  232. 'String', '', ...
  233. 'Callback','rri_select_file({''select_dir''});', ...
  234. 'Tag','DirectoryList');
  235. x = 0.5;
  236. y = 4*line_height + margine;
  237. w = 0.5 - margine;
  238. h = 1 - 7*line_height - 2*margine;
  239. pos = [x y w h];
  240. h_file = uicontrol('Parent',h0, ... % File Listbox
  241. 'Style','listbox', ...
  242. 'Units','normal', ...
  243. 'fontunit','normal', ...
  244. 'FontSize',0.08, ...
  245. 'HorizontalAlignment','left', ...
  246. 'ListboxTop',1, ...
  247. 'Position', pos, ...
  248. 'String', '', ...
  249. 'Callback','rri_select_file({''select_file''});', ...
  250. 'Tag','FileList');
  251. x = margine;
  252. y = 3*line_height + margine - line_height*0.2;
  253. w = 1-2*x;
  254. h = char_height;
  255. pos = [x y w h];
  256. h1 = uicontrol('Parent',h0, ... % Selection Label
  257. 'Style','text', ...
  258. 'Units','normal', ...
  259. 'BackgroundColor',[0.8 0.8 0.8], ...
  260. 'fontunit','normal', ...
  261. 'FontSize',0.5, ...
  262. 'HorizontalAlignment','left', ...
  263. 'Position', pos, ...
  264. 'String','File you selected:', ...
  265. 'Tag','SelectionLabel');
  266. y = 2*line_height + margine;
  267. w = 1-2*x;
  268. pos = [x y w h];
  269. h_select = uicontrol('Parent',h0, ... % Selection Edit
  270. 'Style','edit', ...
  271. 'Units','normal', ...
  272. 'BackgroundColor',[1 1 1], ...
  273. 'fontunit','normal', ...
  274. 'FontSize',0.5, ...
  275. 'HorizontalAlignment','left', ...
  276. 'Position', pos, ...
  277. 'String', '', ...
  278. 'Callback','rri_select_file({''EditSelection''});', ...
  279. 'Tag','SelectionEdit');
  280. x = 2*margine;
  281. y = line_height/2 + margine;
  282. w = 0.2;
  283. h = line_height;
  284. pos = [x y w h];
  285. h_done = uicontrol('Parent',h0, ... % DONE
  286. 'Units','normal', ...
  287. 'fontunit','normal', ...
  288. 'FontSize',0.5, ...
  289. 'ListboxTop',0, ...
  290. 'Position', pos, ...
  291. 'HorizontalAlignment','center', ...
  292. 'String','Save', ... % 'Select', ...
  293. 'Callback','rri_select_file({''DONE_BUTTON_PRESSED''});', ...
  294. 'Tag','DONEButton');
  295. x = 1 - x - w;
  296. pos = [x y w h];
  297. h_cancel = uicontrol('Parent',h0, ... % CANCEL
  298. 'Units','normal', ...
  299. 'fontunit','normal', ...
  300. 'FontSize',0.5, ...
  301. 'ListboxTop',0, ...
  302. 'Position', pos, ...
  303. 'HorizontalAlignment','center', ...
  304. 'String','Cancel', ...
  305. 'Callback','rri_select_file({''CANCEL_BUTTON_PRESSED''});', ...
  306. 'Tag','CANCELButton');
  307. if isempty(dir_name)
  308. dir_name = StartDirectory;
  309. end
  310. set(h_select,'string',dir_name);
  311. filter_select = get(h_filter,'value');
  312. filter_pattern = filter_string(filter_select,:);
  313. setappdata(gcf,'FilterPattern',deblank(filter_pattern));
  314. setappdata(gcf,'filter_string',filter_string);
  315. setappdata(gcf,'h_filter', h_filter);
  316. setappdata(gcf,'h_dir', h_dir);
  317. setappdata(gcf,'h_file', h_file);
  318. setappdata(gcf,'h_select', h_select);
  319. setappdata(gcf,'h_done', h_done);
  320. setappdata(gcf,'h_cancel', h_cancel);
  321. setappdata(gcf,'StartDirectory',StartDirectory);
  322. EditSelection;
  323. h_file = getappdata(gcf,'h_file');
  324. if isempty(get(h_file,'string'))
  325. setappdata(gcf,'ready',0);
  326. else
  327. setappdata(gcf,'ready',1);
  328. end
  329. return; % Init
  330. % called by all the actions, to update 'Directories' or 'Files'
  331. % based on filter_pattern. Select first file in filelist.
  332. %
  333. % --------------------------------------------------------------------
  334. function update_dirlist;
  335. filter_path = getappdata(gcf,'curr_dir');
  336. filter_pattern = getappdata(gcf,'FilterPattern');
  337. if exist(filter_pattern) == 2 % user input specific filename
  338. is_single_file = 1; % need manually take path out later
  339. else
  340. is_single_file = 0;
  341. end
  342. % take the file path out from filter_pattern
  343. %
  344. [fpath fname fext] = fileparts(filter_pattern);
  345. filter_pattern = [fname fext];
  346. dir_struct = dir(filter_path);
  347. if isempty(dir_struct)
  348. msg = 'ERROR: Directory not found!';
  349. uiwait(msgbox(msg,'File Selection Error','modal'));
  350. return;
  351. end;
  352. old_pointer = get(gcf,'Pointer');
  353. set(gcf,'Pointer','watch');
  354. dir_list = dir_struct(find([dir_struct.isdir] == 1));
  355. [sorted_dir_names,sorted_dir_index] = sortrows({dir_list.name}');
  356. dir_struct = dir([filter_path filesep filter_pattern]);
  357. if isempty(dir_struct)
  358. sorted_file_names = [];
  359. else
  360. file_list = dir_struct(find([dir_struct.isdir] == 0));
  361. if is_single_file % take out path
  362. tmp = file_list.name;
  363. [fpath fname fext] = fileparts(tmp);
  364. file_list.name = [fname fext];
  365. end
  366. [sorted_file_names,sorted_file_index] = sortrows({file_list.name}');
  367. end;
  368. disp_dir_names = []; % if need full path, use this
  369. % instead of sorted_dir_names
  370. for i=1:length(sorted_dir_names)
  371. tmp = [filter_path filesep sorted_dir_names{i}];
  372. disp_dir_names = [disp_dir_names {tmp}];
  373. end
  374. h = findobj(gcf,'Tag','DirectoryList');
  375. set(h,'String',sorted_dir_names,'Value',1);
  376. h = findobj(gcf,'Tag','FileList');
  377. set(h,'String',sorted_file_names,'value',1);
  378. h_select = getappdata(gcf,'h_select');
  379. if strcmp(filter_path(end),filesep) % filepath end with filesep
  380. filter_path = filter_path(1:end-1); % take filesep out
  381. end
  382. if isempty(sorted_file_names)
  383. set(h_select,'string',[filter_path filesep]);
  384. else
  385. set(h_select,'string',[filter_path filesep sorted_file_names{1}]);
  386. end
  387. set(gcf,'Pointer',old_pointer);
  388. return; % update_dirlist
  389. % change 'File format':
  390. % update 'Files' & 'File selection' based on file pattern
  391. %
  392. % --------------------------------------------------------------------
  393. function EditFilter()
  394. filter_select = get(gcbo,'value');
  395. filter_string = getappdata(gcf,'filter_string');
  396. filter_pattern = filter_string(filter_select,:);
  397. filter_path = getappdata(gcf,'curr_dir');
  398. % update filter_pattern
  399. setappdata(gcf,'FilterPattern',deblank(filter_pattern));
  400. if isempty(filter_path),
  401. filter_path = filesep;
  402. end;
  403. update_dirlist;
  404. h_file = getappdata(gcf,'h_file');
  405. if isempty(get(h_file,'string'))
  406. setappdata(gcf,'ready',0);
  407. else
  408. setappdata(gcf,'ready',1);
  409. end
  410. return; % EditFilter
  411. % select 'Directories':
  412. % go into the selected dir
  413. % update 'Files' & 'File selection' based on file pattern
  414. %
  415. % --------------------------------------------------------------------
  416. function select_dir()
  417. listed_dir = get(gcbo,'String');
  418. selected_dir_idx = get(gcbo,'Value');
  419. selected_dir = listed_dir{selected_dir_idx};
  420. curr_dir = getappdata(gcf,'curr_dir');
  421. % update the selection box
  422. %
  423. try
  424. cd ([curr_dir filesep selected_dir]);
  425. catch
  426. msg = 'ERROR: Cannot access directory';
  427. uiwait(msgbox(msg,'File Selection Error','modal'));
  428. return;
  429. end;
  430. if isempty(pwd)
  431. curr_dir = filesep;
  432. else
  433. curr_dir = pwd;
  434. end;
  435. setappdata(gcf,'curr_dir',curr_dir);
  436. update_dirlist;
  437. h_file = getappdata(gcf,'h_file');
  438. if isempty(get(h_file,'string'))
  439. setappdata(gcf,'ready',0);
  440. else
  441. setappdata(gcf,'ready',1);
  442. end
  443. return; % select_dir
  444. % select 'Files':
  445. % update 'File selection'
  446. %
  447. % --------------------------------------------------------------------
  448. function select_file()
  449. setappdata(gcf,'ready',1);
  450. listed_file = get(gcbo,'String');
  451. selected_file_idx = get(gcbo,'Value');
  452. selected_file = listed_file{selected_file_idx};
  453. curr_dir = getappdata(gcf,'curr_dir');
  454. if strcmp(curr_dir(end),filesep) % filepath end with filesep
  455. curr_dir = curr_dir(1:end-1); % take filesep out
  456. end
  457. h_select = getappdata(gcf,'h_select');
  458. set(h_select,'string',[curr_dir filesep selected_file]);
  459. return; % select_file
  460. % change 'File selection':
  461. % if it is a file, select that,
  462. % if it is more than a file (*), select those,
  463. % if it is a directory, select based on file pattern
  464. %
  465. % --------------------------------------------------------------------
  466. function EditSelection()
  467. filter_string = getappdata(gcf,'filter_string');
  468. h_select = getappdata(gcf,'h_select');
  469. selected_file = get(h_select,'string');
  470. if exist(selected_file) == 7 % if user enter a dir
  471. setappdata(gcf,'ready',0);
  472. setappdata(gcf,'curr_dir',selected_file); % get new dir
  473. update_dirlist;
  474. else
  475. setappdata(gcf,'ready',1);
  476. [fpath fname fext]= fileparts(selected_file);
  477. if exist(fpath) ~=7 % fpath is not a dir
  478. setappdata(gcf,'ready',0);
  479. msg = 'ERROR: Cannot access directory';
  480. uiwait(msgbox(msg,'File Selection Error','modal'));
  481. end
  482. % if the file format user entered is not supported by matlab
  483. if isempty(strmatch(['*',fext],filter_string,'exact'))
  484. setappdata(gcf,'ready',0);
  485. msg = 'ERROR: File format is not supported by Matlab.';
  486. uiwait(msgbox(msg,'File Selection Error','modal'));
  487. end
  488. end
  489. return; % EditSelection
  490. % --------------------------------------------------------------------
  491. function delete_fig()
  492. try
  493. load('pls_profile');
  494. pls_profile = which('pls_profile.mat');
  495. rri_select_file_pos = get(gcbf,'position');
  496. save(pls_profile, '-append', 'rri_select_file_pos');
  497. catch
  498. end
  499. return;