-
Notifications
You must be signed in to change notification settings - Fork 0
/
uipickfiles.m
1557 lines (1447 loc) · 45.7 KB
/
uipickfiles.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function out = uipickfiles(varargin)
%uipickfiles: GUI program to select files and/or folders.
%
% Syntax:
% files = uipickfiles('PropertyName',PropertyValue,...)
%
% The current folder can be changed by operating in the file navigator:
% double-clicking on a folder in the list or pressing Enter to move further
% down the tree, using the popup menu, clicking the up arrow button or
% pressing Backspace to move up the tree, typing a path in the box to move
% to any folder or right-clicking (control-click on Mac) on the path box to
% revisit a previously-visited folder. These folders are listed in order
% of when they were last visited (most recent at the top) and the list is
% saved between calls to uipickfiles. The list can be cleared or its
% maximum length changed with the items at the bottom of the menu.
% (Windows only: To go to a UNC-named resource you will have to type the
% UNC name in the path box, but all such visited resources will be
% remembered and listed along with the mapped drives.) The items in the
% file navigator can be sorted by name, modification date or size by
% clicking on the headers, though neither date nor size are displayed. All
% folders have zero size.
%
% Files can be added to the list by double-clicking or selecting files
% (non-contiguous selections are possible with the control key) and
% pressing the Add button. Control-F will select all the files listed in
% the navigator while control-A will select everything (Command instead of
% Control on the Mac). Since double-clicking a folder will open it,
% folders can be added only by selecting them and pressing the Add button.
% Files/folders in the list can be removed or re-ordered. Recall button
% will insert into the Selected Files list whatever files were returned the
% last time uipickfiles was run. When finished, a press of the Done button
% will return the full paths to the selected items in a cell array,
% structure array or character array. If the Cancel button or the escape
% key is pressed then zero is returned.
%
% The figure can be moved and resized in the usual way and this position is
% saved and used for subsequent calls to uipickfiles. The default position
% can be restored by double-clicking in a vacant region of the figure.
%
% The following optional property/value pairs can be specified as arguments
% to control the indicated behavior:
%
% Property Value
% ---------- ----------------------------------------------------------
% FilterSpec String to specify starting folder and/or file filter.
% Ex: 'C:\bin' will start up in that folder. '*.txt'
% will list only files ending in '.txt'. 'c:\bin\*.txt' will
% do both. Default is to start up in the current folder and
% list all files. Can be changed with the GUI.
%
% REFilter String containing a regular expression used to filter the
% file list. Ex: '\.m$|\.mat$' will list files ending in
% '.m' and '.mat'. Default is empty string. Can be used
% with FilterSpec and both filters are applied. Can be
% changed with the GUI.
%
% REDirs Logical flag indicating whether to apply the regular
% expression filter to folder names. Default is false which
% means that all folders are listed. Can be changed with the
% GUI.
%
% Type Two-column cell array where the first column contains file
% filters and the second column contains descriptions. If
% this property is specified an additional popup menu will
% appear below the File Filter and selecting an item will put
% that item into the File Filter. By default, the first item
% will be entered into the File Filter. For example,
% { '*.m', 'M-files' ;
% '*.mat', 'MAT-files' }.
% Can also be a cell vector of file filter strings in which
% case the descriptions will be the same as the file filters
% themselves.
% Must be a cell array even if there is only one entry.
%
% Prompt String containing a prompt appearing in the title bar of
% the figure. Default is 'Select files'.
%
% NumFiles Scalar or vector specifying number of files that must be
% selected. A scalar specifies an exact value; a two-element
% vector can be used to specify a range, [min max]. The
% function will not return unless the specified number of
% files have been chosen. Default is [] which accepts any
% number of files.
%
% Append Cell array of strings, structure array or char array
% containing a previously returned output from uipickfiles.
% Used to start up program with some entries in the Selected
% Files list. Any included files that no longer exist will
% not appear. Default is empty cell array, {}.
%
% Output String specifying the data type of the output: 'cell',
% 'struct' or 'char'. Specifying 'cell' produces a cell
% array of strings, the strings containing the full paths of
% the chosen files. 'Struct' returns a structure array like
% the result of the dir function except that the 'name' field
% contains a full path instead of just the file name. 'Char'
% returns a character array of the full paths. This is most
% useful when you have just one file and want it in a string
% instead of a cell array containing just one string. The
% default is 'cell'.
%
% All properties and values are case-insensitive and need only be
% unambiguous. For example,
%
% files = uipickfiles('num',1,'out','ch')
%
% is valid usage.
% Version: 1.15, 2 March 2012
% Author: Douglas M. Schwarz
% Email: dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
% Real_email = regexprep(Email,{'=','*'},{'@','.'})
% Define properties and set default values.
prop.filterspec = '*';
prop.refilter = '';
prop.redirs = false;
prop.type = {};
prop.prompt = 'Select files';
prop.numfiles = [];
prop.append = [];
prop.output = 'cell';
% Process inputs and set prop fields.
prop = parsepropval(prop,varargin{:});
% Validate FilterSpec property.
if isempty(prop.filterspec)
prop.filterspec = '*';
end
if ~ischar(prop.filterspec)
error('FilterSpec property must contain a string.')
end
% Validate REFilter property.
if ~ischar(prop.refilter)
error('REFilter property must contain a string.')
end
% Validate REDirs property.
if ~isscalar(prop.redirs)
error('REDirs property must contain a scalar.')
end
% Validate Type property.
if isempty(prop.type)
elseif iscellstr(prop.type) && isscalar(prop.type)
prop.type = repmat(prop.type(:),1,2);
elseif iscellstr(prop.type) && size(prop.type,2) == 2
else
error(['Type property must be empty or a cellstr vector or ',...
'a 2-column cellstr matrix.'])
end
% Validate Prompt property.
if ~ischar(prop.prompt)
error('Prompt property must contain a string.')
end
% Validate NumFiles property.
if numel(prop.numfiles) > 2 || any(prop.numfiles < 0)
error('NumFiles must be empty, a scalar or two-element vector.')
end
prop.numfiles = unique(prop.numfiles);
if isequal(prop.numfiles,1)
numstr = 'Select exactly 1 file.';
elseif length(prop.numfiles) == 1
numstr = sprintf('Select exactly %d items.',prop.numfiles);
else
numstr = sprintf('Select %d to %d items.',prop.numfiles);
end
% Validate Append property and initialize pick data.
if isstruct(prop.append) && isfield(prop.append,'name')
prop.append = {prop.append.name};
elseif ischar(prop.append)
prop.append = cellstr(prop.append);
end
if isempty(prop.append)
file_picks = {};
full_file_picks = {};
dir_picks = dir(' '); % Create empty directory structure.
elseif iscellstr(prop.append) && isvector(prop.append)
num_items = length(prop.append);
file_picks = cell(1,num_items);
full_file_picks = cell(1,num_items);
dir_fn = fieldnames(dir(' '));
dir_picks = repmat(cell2struct(cell(length(dir_fn),1),dir_fn(:)),...
num_items,1);
for item = 1:num_items
if exist(prop.append{item},'dir') && ...
~any(strcmp(full_file_picks,prop.append{item}))
full_file_picks{item} = prop.append{item};
[unused,fn,ext] = fileparts(prop.append{item});
file_picks{item} = [fn,ext];
temp = dir(fullfile(prop.append{item},'..'));
if ispc || ismac
thisdir = strcmpi({temp.name},[fn,ext]);
else
thisdir = strcmp({temp.name},[fn,ext]);
end
dir_picks(item) = temp(thisdir);
dir_picks(item).name = prop.append{item};
elseif exist(prop.append{item},'file') && ...
~any(strcmp(full_file_picks,prop.append{item}))
full_file_picks{item} = prop.append{item};
[unused,fn,ext] = fileparts(prop.append{item});
file_picks{item} = [fn,ext];
dir_picks(item) = dir(prop.append{item});
dir_picks(item).name = prop.append{item};
else
continue
end
end
% Remove items which no longer exist.
missing = cellfun(@isempty,full_file_picks);
full_file_picks(missing) = [];
file_picks(missing) = [];
dir_picks(missing) = [];
else
error('Append must be a cell, struct or char array.')
end
% Validate Output property.
legal_outputs = {'cell','struct','char'};
out_idx = find(strncmpi(prop.output,legal_outputs,length(prop.output)));
if length(out_idx) == 1
prop.output = legal_outputs{out_idx};
else
error(['Value of ''Output'' property, ''%s'', is illegal or '...
'ambiguous.'],prop.output)
end
% Set style preference for display of folders.
% 1 => folder icon before and filesep after
% 2 => bullet before and filesep after
% 3 => filesep after only
folder_style_pref = 1;
fsdata = set_folder_style(folder_style_pref);
% Initialize file lists.
if exist(prop.filterspec,'dir')
current_dir = prop.filterspec;
filter = '*';
else
[current_dir,f,e] = fileparts(prop.filterspec);
filter = [f,e];
end
if isempty(current_dir)
current_dir = pwd;
end
if isempty(filter)
filter = '*';
end
re_filter = prop.refilter;
full_filter = fullfile(current_dir,filter);
network_volumes = {};
[path_cell,new_network_vol] = path2cell(current_dir);
if exist(new_network_vol,'dir')
network_volumes = unique([network_volumes,{new_network_vol}]);
end
fdir = filtered_dir(full_filter,re_filter,prop.redirs,...
@(x)file_sort(x,[1 0 0]));
filenames = {fdir.name}';
filenames = annotate_file_names(filenames,fdir,fsdata);
% Initialize some data.
show_full_path = false;
nodupes = true;
% Get history preferences and set history.
history = getpref('uipickfiles','history',...
struct('name',current_dir,'time',now));
default_history_size = 15;
history_size = getpref('uipickfiles','history_size',default_history_size);
history = update_history(history,current_dir,now,history_size);
% Get figure position preference and create figure.
gray = get(0,'DefaultUIControlBackgroundColor');
if ispref('uipickfiles','figure_position');
fig_pos = getpref('uipickfiles','figure_position');
fig = figure('Position',fig_pos,...
'Color',gray,...
'MenuBar','none',...
'WindowStyle','modal',...
'Resize','on',...
'NumberTitle','off',...
'Name',prop.prompt,...
'IntegerHandle','off',...
'CloseRequestFcn',@cancel,...
'ButtonDownFcn',@reset_figure_size,...
'KeyPressFcn',@keypressmisc,...
'Visible','off');
else
fig_pos = [0 0 740 494];
fig = figure('Position',fig_pos,...
'Color',gray,...
'MenuBar','none',...
'WindowStyle','modal',...
'Resize','on',...
'NumberTitle','off',...
'Name',prop.prompt,...
'IntegerHandle','off',...
'CloseRequestFcn',@cancel,...
'CreateFcn',{@movegui,'center'},...
'ButtonDownFcn',@reset_figure_size,...
'KeyPressFcn',@keypressmisc,...
'Visible','off');
end
% Set system-dependent items.
if ismac
set(fig,'DefaultUIControlFontName','Lucida Grande')
set(fig,'DefaultUIControlFontSize',9)
sort_ctrl_size = 8;
mod_key = 'command';
action = 'Control-click';
elseif ispc
set(fig,'DefaultUIControlFontName','Tahoma')
set(fig,'DefaultUIControlFontSize',8)
sort_ctrl_size = 7;
mod_key = 'control';
action = 'Right-click';
else
sort_ctrl_size = get(fig,'DefaultUIControlFontSize') - 1;
mod_key = 'control';
action = 'Right-click';
end
% Create uicontrols.
frame1 = uicontrol('Style','frame',...
'Position',[255 260 110 70]);
frame2 = uicontrol('Style','frame',...
'Position',[275 135 110 100]);
navlist = uicontrol('Style','listbox',...
'Position',[10 10 250 320],...
'String',filenames,...
'Value',[],...
'BackgroundColor','w',...
'Callback',@clicknav,...
'KeyPressFcn',@keypressnav,...
'Max',2);
tri_up = repmat([1 1 1 1 0 1 1 1 1;1 1 1 0 0 0 1 1 1;1 1 0 0 0 0 0 1 1;...
1 0 0 0 0 0 0 0 1],[1 1 3]);
tri_up(tri_up == 1) = NaN;
tri_down = tri_up(end:-1:1,:,:);
tri_null = NaN(4,9,3);
tri_icon = {tri_down,tri_null,tri_up};
sort_state = [1 0 0];
last_sort_state = [1 1 1];
sort_cb = zeros(1,3);
sort_cb(1) = uicontrol('Style','checkbox',...
'Position',[15 331 70 15],...
'String','Name',...
'FontSize',sort_ctrl_size,...
'Value',sort_state(1),...
'CData',tri_icon{sort_state(1)+2},...
'KeyPressFcn',@keypressmisc,...
'Callback',{@sort_type,1});
sort_cb(2) = uicontrol('Style','checkbox',...
'Position',[85 331 70 15],...
'String','Date',...
'FontSize',sort_ctrl_size,...
'Value',sort_state(2),...
'CData',tri_icon{sort_state(2)+2},...
'KeyPressFcn',@keypressmisc,...
'Callback',{@sort_type,2});
sort_cb(3) = uicontrol('Style','checkbox',...
'Position',[155 331 70 15],...
'String','Size',...
'FontSize',sort_ctrl_size,...
'Value',sort_state(3),...
'CData',tri_icon{sort_state(3)+2},...
'KeyPressFcn',@keypressmisc,...
'Callback',{@sort_type,3});
pickslist = uicontrol('Style','listbox',...
'Position',[380 10 350 320],...
'String',file_picks,...
'BackgroundColor','w',...
'Callback',@clickpicks,...
'KeyPressFcn',@keypresslist,...
'Max',2,...
'Value',[]);
openbut = uicontrol('Style','pushbutton',...
'Position',[270 300 80 20],...
'String','Open',...
'Enable','off',...
'KeyPressFcn',@keypressmisc,...
'Callback',@open);
arrow = [ ...
' 1 ';
' 10 ';
' 10 ';
'000000000000';
' 10 ';
' 10 ';
' 1 '];
cmap = NaN(128,3);
cmap(double('10'),:) = [0.5 0.5 0.5;0 0 0];
arrow_im = NaN(7,76,3);
arrow_im(:,45:56,:) = ind2rgb(double(arrow),cmap);
addbut = uicontrol('Style','pushbutton',...
'Position',[270 270 80 20],...
'String','Add ',...
'Enable','off',...
'CData',arrow_im,...
'KeyPressFcn',@keypressmisc,...
'Callback',@add);
removebut = uicontrol('Style','pushbutton',...
'Position',[290 205 80 20],...
'String','Remove',...
'Enable','off',...
'KeyPressFcn',@keypressmisc,...
'Callback',@remove);
moveupbut = uicontrol('Style','pushbutton',...
'Position',[290 175 80 20],...
'String','Move Up',...
'Enable','off',...
'KeyPressFcn',@keypressmisc,...
'Callback',@moveup);
movedownbut = uicontrol('Style','pushbutton',...
'Position',[290 145 80 20],...
'String','Move Down',...
'Enable','off',...
'KeyPressFcn',@keypressmisc,...
'Callback',@movedown);
dir_popup = uicontrol('Style','popupmenu',...
'Position',[10 350 225 20],...
'BackgroundColor','w',...
'String',path_cell,...
'Value',length(path_cell),...
'KeyPressFcn',@keypressmisc,...
'Callback',@dirpopup);
uparrow = [ ...
' 0 ';
' 000 ';
'00000 ';
' 0 ';
' 0 ';
' 0 ';
' 000000'];
cmap = NaN(128,3);
cmap(double('0'),:) = [0 0 0];
uparrow_im = ind2rgb(double(uparrow),cmap);
up_dir_but = uicontrol('Style','pushbutton',...
'Position',[240 350 20 20],...
'CData',uparrow_im,...
'KeyPressFcn',@keypressmisc,...
'Callback',@dir_up_one,...
'ToolTip','Go to parent folder');
if length(path_cell) > 1
set(up_dir_but','Enable','on')
else
set(up_dir_but','Enable','off')
end
hist_cm = uicontextmenu;
pathbox = uicontrol('Style','edit',...
'Position',[10 375 250 26],...
'BackgroundColor','w',...
'String',current_dir,...
'HorizontalAlignment','left',...
'TooltipString',[action,' to display folder history'],...
'KeyPressFcn',@keypressmisc,...
'Callback',@change_path,...
'UIContextMenu',hist_cm);
label1 = uicontrol('Style','text',...
'Position',[10 401 250 16],...
'String','Current Folder',...
'HorizontalAlignment','center',...
'TooltipString',[action,' to display folder history'],...
'UIContextMenu',hist_cm);
hist_menus = [];
make_history_cm()
label2 = uicontrol('Style','text',...
'Position',[10 440+36 80 17],...
'String','File Filter',...
'HorizontalAlignment','left');
label3 = uicontrol('Style','text',...
'Position',[100 440+36 160 17],...
'String','Reg. Exp. Filter',...
'HorizontalAlignment','left');
showallfiles = uicontrol('Style','checkbox',...
'Position',[270 420+32 110 20],...
'String','Show All Files',...
'Value',0,...
'HorizontalAlignment','left',...
'KeyPressFcn',@keypressmisc,...
'Callback',@togglefilter);
refilterdirs = uicontrol('Style','checkbox',...
'Position',[270 420+10 100 20],...
'String','RE Filter Dirs',...
'Value',prop.redirs,...
'HorizontalAlignment','left',...
'KeyPressFcn',@keypressmisc,...
'Callback',@toggle_refiltdirs);
filter_ed = uicontrol('Style','edit',...
'Position',[10 420+30 80 26],...
'BackgroundColor','w',...
'String',filter,...
'HorizontalAlignment','left',...
'KeyPressFcn',@keypressmisc,...
'Callback',@setfilspec);
refilter_ed = uicontrol('Style','edit',...
'Position',[100 420+30 160 26],...
'BackgroundColor','w',...
'String',re_filter,...
'HorizontalAlignment','left',...
'KeyPressFcn',@keypressmisc,...
'Callback',@setrefilter);
type_value = 1;
type_popup = uicontrol('Style','popupmenu',...
'Position',[10 422 250 20],...
'String','',...
'BackgroundColor','w',...
'Value',type_value,...
'KeyPressFcn',@keypressmisc,...
'Callback',@filter_type_callback,...
'Visible','off');
if ~isempty(prop.type)
set(filter_ed,'String',prop.type{type_value,1})
setfilspec()
set(type_popup,'String',prop.type(:,2),'Visible','on')
end
viewfullpath = uicontrol('Style','checkbox',...
'Position',[380 335 230 20],...
'String','Show full paths',...
'Value',show_full_path,...
'HorizontalAlignment','left',...
'KeyPressFcn',@keypressmisc,...
'Callback',@showfullpath);
remove_dupes = uicontrol('Style','checkbox',...
'Position',[380 360 280 20],...
'String','Remove duplicates (as per full path)',...
'Value',nodupes,...
'HorizontalAlignment','left',...
'KeyPressFcn',@keypressmisc,...
'Callback',@removedupes);
recall_button = uicontrol('Style','pushbutton',...
'Position',[665 335 65 20],...
'String','Recall',...
'KeyPressFcn',@keypressmisc,...
'Callback',@recall,...
'ToolTip','Add previously selected items');
label4 = uicontrol('Style','text',...
'Position',[380 405 350 20],...
'String','Selected Items',...
'HorizontalAlignment','center');
done_button = uicontrol('Style','pushbutton',...
'Position',[280 80 80 30],...
'String','Done',...
'KeyPressFcn',@keypressmisc,...
'Callback',@done);
cancel_button = uicontrol('Style','pushbutton',...
'Position',[280 30 80 30],...
'String','Cancel',...
'KeyPressFcn',@keypressmisc,...
'Callback',@cancel);
% If necessary, add warning about number of items to be selected.
num_files_warn = uicontrol('Style','text',...
'Position',[380 385 350 16],...
'String',numstr,...
'ForegroundColor',[0.8 0 0],...
'HorizontalAlignment','center',...
'Visible','off');
if ~isempty(prop.numfiles)
set(num_files_warn,'Visible','on')
end
resize()
% Make figure visible and hide handle.
set(fig,'HandleVisibility','off',...
'Visible','on',...
'ResizeFcn',@resize)
% Wait until figure is closed.
uiwait(fig)
% Compute desired output.
switch prop.output
case 'cell'
out = full_file_picks;
case 'struct'
out = dir_picks(:);
case 'char'
out = char(full_file_picks);
case 'cancel'
out = 0;
end
% Update history preference.
setpref('uipickfiles','history',history)
if ~isempty(full_file_picks) && ~strcmp(prop.output,'cancel')
setpref('uipickfiles','full_file_picks',full_file_picks)
end
% Update figure position preference.
setpref('uipickfiles','figure_position',fig_pos)
% ----------------- Callback nested functions ----------------
function add(varargin)
values = get(navlist,'Value');
for i = 1:length(values)
dir_pick = fdir(values(i));
pick = dir_pick.name;
pick_full = fullfile(current_dir,pick);
dir_pick.name = pick_full;
if ~nodupes || ~any(strcmp(full_file_picks,pick_full))
file_picks{end + 1} = pick; %#ok<AGROW>
full_file_picks{end + 1} = pick_full; %#ok<AGROW>
dir_picks(end + 1) = dir_pick; %#ok<AGROW>
end
end
if show_full_path
set(pickslist,'String',full_file_picks,'Value',[]);
else
set(pickslist,'String',file_picks,'Value',[]);
end
set([removebut,moveupbut,movedownbut],'Enable','off');
end
function remove(varargin)
values = get(pickslist,'Value');
file_picks(values) = [];
full_file_picks(values) = [];
dir_picks(values) = [];
top = get(pickslist,'ListboxTop');
num_above_top = sum(values < top);
top = top - num_above_top;
num_picks = length(file_picks);
new_value = min(min(values) - num_above_top,num_picks);
if num_picks == 0
new_value = [];
set([removebut,moveupbut,movedownbut],'Enable','off')
end
if show_full_path
set(pickslist,'String',full_file_picks,'Value',new_value,...
'ListboxTop',top)
else
set(pickslist,'String',file_picks,'Value',new_value,...
'ListboxTop',top)
end
end
function open(varargin)
values = get(navlist,'Value');
if fdir(values).isdir
set(fig,'pointer','watch')
drawnow
% Convert 'My Documents' to 'Documents' when necessary.
if ispc && strcmp(fdir(values).name,'My Documents')
if isempty(dir(fullfile(current_dir,fdir(values).name)))
values = find(strcmp({fdir.name},'Documents'));
end
end
current_dir = fullfile(current_dir,fdir(values).name);
history = update_history(history,current_dir,now,history_size);
make_history_cm()
full_filter = fullfile(current_dir,filter);
path_cell = path2cell(current_dir);
fdir = filtered_dir(full_filter,re_filter,prop.redirs,...
@(x)file_sort(x,sort_state));
filenames = {fdir.name}';
filenames = annotate_file_names(filenames,fdir,fsdata);
set(dir_popup,'String',path_cell,'Value',length(path_cell))
if length(path_cell) > 1
set(up_dir_but','Enable','on')
else
set(up_dir_but','Enable','off')
end
set(pathbox,'String',current_dir)
set(navlist,'ListboxTop',1,'Value',[],'String',filenames)
set(addbut,'Enable','off')
set(openbut,'Enable','off')
set(fig,'pointer','arrow')
end
end
function clicknav(varargin)
value = get(navlist,'Value');
nval = length(value);
dbl_click_fcn = @add;
switch nval
case 0
set([addbut,openbut],'Enable','off')
case 1
set(addbut,'Enable','on');
if fdir(value).isdir
set(openbut,'Enable','on')
dbl_click_fcn = @open;
else
set(openbut,'Enable','off')
end
otherwise
set(addbut,'Enable','on')
set(openbut,'Enable','off')
end
if strcmp(get(fig,'SelectionType'),'open')
dbl_click_fcn();
end
end
function keypressmisc(h,evt) %#ok<INUSL>
if strcmp(evt.Key,'escape') && isequal(evt.Modifier,cell(1,0))
% Escape key means Cancel.
cancel()
end
end
function keypressnav(h,evt) %#ok<INUSL>
if length(path_cell) > 1 && strcmp(evt.Key,'backspace') && ...
isequal(evt.Modifier,cell(1,0))
% Backspace means go to parent folder.
dir_up_one()
elseif strcmp(evt.Key,'f') && isequal(evt.Modifier,{mod_key})
% Control-F (Command-F on Mac) means select all files.
value = find(~[fdir.isdir]);
set(navlist,'Value',value)
elseif strcmp(evt.Key,'rightarrow') && ...
isequal(evt.Modifier,cell(1,0))
% Right arrow key means select the file.
add()
elseif strcmp(evt.Key,'escape') && isequal(evt.Modifier,cell(1,0))
% Escape key means Cancel.
cancel()
end
end
function keypresslist(h,evt) %#ok<INUSL>
if strcmp(evt.Key,'backspace') && isequal(evt.Modifier,cell(1,0))
% Backspace means remove item from list.
remove()
elseif strcmp(evt.Key,'escape') && isequal(evt.Modifier,cell(1,0))
% Escape key means Cancel.
cancel()
end
end
function clickpicks(varargin)
value = get(pickslist,'Value');
if isempty(value)
set([removebut,moveupbut,movedownbut],'Enable','off')
else
set(removebut,'Enable','on')
if min(value) == 1
set(moveupbut,'Enable','off')
else
set(moveupbut,'Enable','on')
end
if max(value) == length(file_picks)
set(movedownbut,'Enable','off')
else
set(movedownbut,'Enable','on')
end
end
if strcmp(get(fig,'SelectionType'),'open')
remove();
end
end
function recall(varargin)
if ispref('uipickfiles','full_file_picks')
ffp = getpref('uipickfiles','full_file_picks');
else
ffp = {};
end
for i = 1:length(ffp)
if exist(ffp{i},'dir') && ...
(~nodupes || ~any(strcmp(full_file_picks,ffp{i})))
full_file_picks{end + 1} = ffp{i}; %#ok<AGROW>
[unused,fn,ext] = fileparts(ffp{i});
file_picks{end + 1} = [fn,ext]; %#ok<AGROW>
temp = dir(fullfile(ffp{i},'..'));
if ispc || ismac
thisdir = strcmpi({temp.name},[fn,ext]);
else
thisdir = strcmp({temp.name},[fn,ext]);
end
dir_picks(end + 1) = temp(thisdir); %#ok<AGROW>
dir_picks(end).name = ffp{i};
elseif exist(ffp{i},'file') && ...
(~nodupes || ~any(strcmp(full_file_picks,ffp{i})))
full_file_picks{end + 1} = ffp{i}; %#ok<AGROW>
[unused,fn,ext] = fileparts(ffp{i});
file_picks{end + 1} = [fn,ext]; %#ok<AGROW>
dir_picks(end + 1) = dir(ffp{i}); %#ok<AGROW>
dir_picks(end).name = ffp{i};
end
end
if show_full_path
set(pickslist,'String',full_file_picks,'Value',[]);
else
set(pickslist,'String',file_picks,'Value',[]);
end
set([removebut,moveupbut,movedownbut],'Enable','off');
end
function sort_type(h,evt,cb) %#ok<INUSL>
if sort_state(cb)
sort_state(cb) = -sort_state(cb);
last_sort_state(cb) = sort_state(cb);
else
sort_state = zeros(1,3);
sort_state(cb) = last_sort_state(cb);
end
set(sort_cb,{'CData'},tri_icon(sort_state + 2)')
fdir = filtered_dir(full_filter,re_filter,prop.redirs,...
@(x)file_sort(x,sort_state));
filenames = {fdir.name}';
filenames = annotate_file_names(filenames,fdir,fsdata);
set(dir_popup,'String',path_cell,'Value',length(path_cell))
if length(path_cell) > 1
set(up_dir_but','Enable','on')
else
set(up_dir_but','Enable','off')
end
set(pathbox,'String',current_dir)
set(navlist,'String',filenames,'Value',[])
set(addbut,'Enable','off')
set(openbut,'Enable','off')
set(fig,'pointer','arrow')
end
function dirpopup(varargin)
value = get(dir_popup,'Value');
container = path_cell{min(value + 1,length(path_cell))};
path_cell = path_cell(1:value);
set(fig,'pointer','watch')
drawnow
if ispc && value == 1
current_dir = '';
full_filter = filter;
drives = getdrives(network_volumes);
num_drives = length(drives);
temp = tempname;
mkdir(temp)
dir_temp = dir(temp);
rmdir(temp)
fdir = repmat(dir_temp(1),num_drives,1);
[fdir.name] = deal(drives{:});
else
current_dir = cell2path(path_cell);
history = update_history(history,current_dir,now,history_size);
make_history_cm()
full_filter = fullfile(current_dir,filter);
fdir = filtered_dir(full_filter,re_filter,prop.redirs,...
@(x)file_sort(x,sort_state));
end
filenames = {fdir.name}';
selected = find(strcmp(filenames,container));
filenames = annotate_file_names(filenames,fdir,fsdata);
set(dir_popup,'String',path_cell,'Value',length(path_cell))
if length(path_cell) > 1
set(up_dir_but','Enable','on')
else
set(up_dir_but','Enable','off')
end
set(pathbox,'String',current_dir)
set(navlist,'String',filenames,'Value',selected)
set(addbut,'Enable','off')
set(fig,'pointer','arrow')
end
function dir_up_one(varargin)
value = length(path_cell) - 1;
container = path_cell{value + 1};
path_cell = path_cell(1:value);
set(fig,'pointer','watch')
drawnow
if ispc && value == 1
current_dir = '';
full_filter = filter;
drives = getdrives(network_volumes);
num_drives = length(drives);
temp = tempname;
mkdir(temp)
dir_temp = dir(temp);
rmdir(temp)
fdir = repmat(dir_temp(1),num_drives,1);
[fdir.name] = deal(drives{:});
else
current_dir = cell2path(path_cell);
history = update_history(history,current_dir,now,history_size);
make_history_cm()
full_filter = fullfile(current_dir,filter);
fdir = filtered_dir(full_filter,re_filter,prop.redirs,...
@(x)file_sort(x,sort_state));
end
filenames = {fdir.name}';
selected = find(strcmp(filenames,container));
filenames = annotate_file_names(filenames,fdir,fsdata);
set(dir_popup,'String',path_cell,'Value',length(path_cell))
if length(path_cell) > 1
set(up_dir_but','Enable','on')
else
set(up_dir_but','Enable','off')
end
set(pathbox,'String',current_dir)
set(navlist,'String',filenames,'Value',selected)
set(addbut,'Enable','off')
set(fig,'pointer','arrow')
end
function change_path(varargin)
set(fig,'pointer','watch')
drawnow
proposed_path = get(pathbox,'String');
% Process any folders named '..'.
proposed_path_cell = path2cell(proposed_path);
ddots = strcmp(proposed_path_cell,'..');
ddots(find(ddots) - 1) = true;
proposed_path_cell(ddots) = [];
proposed_path = cell2path(proposed_path_cell);
% Check for existance of folder.
if ~exist(proposed_path,'dir')
set(fig,'pointer','arrow')
uiwait(errordlg(['Folder "',proposed_path,...
'" does not exist.'],'','modal'))
return
end
current_dir = proposed_path;
history = update_history(history,current_dir,now,history_size);
make_history_cm()
full_filter = fullfile(current_dir,filter);
[path_cell,new_network_vol] = path2cell(current_dir);
if exist(new_network_vol,'dir')
network_volumes = unique([network_volumes,{new_network_vol}]);
end
fdir = filtered_dir(full_filter,re_filter,prop.redirs,...
@(x)file_sort(x,sort_state));
filenames = {fdir.name}';
filenames = annotate_file_names(filenames,fdir,fsdata);
set(dir_popup,'String',path_cell,'Value',length(path_cell))
if length(path_cell) > 1
set(up_dir_but','Enable','on')
else
set(up_dir_but','Enable','off')
end
set(pathbox,'String',current_dir)
set(navlist,'String',filenames,'Value',[])
set(addbut,'Enable','off')
set(openbut,'Enable','off')
set(fig,'pointer','arrow')
end
function showfullpath(varargin)
show_full_path = get(viewfullpath,'Value');
if show_full_path
set(pickslist,'String',full_file_picks)
else
set(pickslist,'String',file_picks)
end
end
function removedupes(varargin)
nodupes = get(remove_dupes,'Value');
if nodupes
num_picks = length(full_file_picks);
[unused,rev_order] = unique(full_file_picks(end:-1:1)); %#ok<SETNU>
order = sort(num_picks + 1 - rev_order);
full_file_picks = full_file_picks(order);
file_picks = file_picks(order);
dir_picks = dir_picks(order);
if show_full_path
set(pickslist,'String',full_file_picks,'Value',[])
else
set(pickslist,'String',file_picks,'Value',[])
end
set([removebut,moveupbut,movedownbut],'Enable','off')
end
end
function moveup(varargin)
value = get(pickslist,'Value');
set(removebut,'Enable','on')
n = length(file_picks);
omega = 1:n;
index = zeros(1,n);
index(value - 1) = omega(value);
index(setdiff(omega,value - 1)) = omega(setdiff(omega,value));
file_picks = file_picks(index);
full_file_picks = full_file_picks(index);
dir_picks = dir_picks(index);