-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdep.m
3867 lines (3644 loc) · 92.7 KB
/
fdep.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
%FDEP to show a function's dependencies
%
% FDEP dissects a MATLAB (ML) file and iteratively looks for
% all user defined functions (modules), which are used
% during runtime
%
% FDEP retrieves for each module the exacty syntax of its
% - main function
% - subfunctions
% - nested functions
% - anonymous functions
% - eval class calls
% - unresolved calls
% and all
% - ML stock functions
% - ML built-in functions
% - ML classes
% - ML toolboxes
% that it uses
%
% runtime options and returned macros create user-friendly,
% comprehensible, and interactive GUIs, which
% - list the results in various panels
% - plot a full dependency matrix
%
% in essence, FDEP is a wrapper for DEPFUN and MLINT;
% however, due to an efficient pruning engine
% it is considerably faster
%
% see also: depfun, depdir, ckdepfun, mlint, which
% and the accompanying HTML file
%
%SYNTAX
%-------------------------------------------------------------------------------
% P = FDEP(FNAM);
% P = FDEP(FNAM,OPT1,OPT2,...);
%
%INPUT
%-------------------------------------------------------------------------------
% FNAM : M-file (function or script) or P-file
% - only ML entities can be extracted from
% standalone P-files, which do NOT have a
% corresponding M-file
%
% OPT : description
% --------------------------------------------
% -q : do NOT show runtime processing
% -l : show module list
% -m : show dependency matrix
%
%OUTPUT
%-------------------------------------------------------------------------------
% P a structure, which returns all information from the lex parser;
% fields, which are of common interest, are these macros
%
% P.macro() call macro with default args
% P.macro(arg,...) call macro with arguments arg1,...
%
% arg description
% --------------------------------------------------------
% .help () show help for the listing panels in a window
% 1 - show help in the command window
% .list () create the GUI that lists the results
% M - activate module M
% .find M1,... show the synopsis of modules M1,...
% .get M1,... retrieve all data of modules M1,...
% .mhelp M1,... show all data of module M1,... in a window
% .mplot () create the GUI that shows the dependency matrix
% N1,... - show nodes N1,...
% numeric input syntax ([M#column/M#row],...)
% only valid nodes are shown with guiding lines
% .tplot () show the runtime and modules tree in a window
%
% Mx may be numeric or the name of an existing module
% or cells with any combination of the above
%
% for more help, see the accompanying HTML file
%
%EXAMPLE
%-------------------------------------------------------------------------------
% p=fdep('myfile');
% p.list(); % show module list
% p.mplot(); % show dependency matrix
% p.find(2); % show summary of module #2
% p.mhelp(3,'m'); % show synopsis of modules #3 and 'm'
% d=p.get('n'); % retrieve data of module <n>
% for software developers
%
% mn=2; % module number
% access module data
% p.fun(mn) % module name
% p.fun(p.mix{mn}) % calls TO
% p.fun(p.cix{mn}) % calls FROM
% access ML data
% fn=1 % stock function group
% p.mlfun{fn}(p.mlix{sn,fn})
%{
TEST r2007a
[z{1:8,1}]=depfun(p.module(v),'-toponly','-quiet');
z{1}
mlint(p.module(v),'-a','-calls')
%}
% created:
% us 07-Mar-2006
% modified:
% us 21-Jun-2010 02:16:53 / FEX R2008a
%-------------------------------------------------------------------------------
function po=fdep(varargin)
magic='FDEP';
ver='21-Jun-2010 02:16:53';
dopt={
'-toponly'
'-quiet'
};
mopt={
'-m3'
'-calls'
};
if nargout
po=[];
end
if ~nargin
if nargout
po=FDEP_ini_engine(ver,magic,dopt,mopt,varargin{:});
return;
else
help(mfilename);
clear po;
end
return;
end
[p,par]=FDEP_ini_engine(ver,magic,dopt,mopt,varargin{:});
if isempty(par)
return;
end
tim=clock;
[p,par]=FDEP_get_dep(p,par,p.fun{1});
p.runtime(1)=etime(clock,tim);
tim=clock;
[p,par]=FDEP_end_engine(p,par);
p.runtime(2)=etime(clock,tim);
% needed to make sure all macros contain the latest parameters!
tim=clock;
p=FDEP_flib(magic,p,2);
if par.opt.lflg
p=p.list();
p=FDEP_flib(magic,p,1); % needed!
end
if par.opt.mflg
p=p.mplot();
p=FDEP_flib(magic,p,1); % needed
end
p.runtime(3)=etime(clock,tim);
% no user-defined dependencies
if ~p.ncall &&...
~par.opt.qflg
disp(sprintf('\n------------- NO USER-DEFINED DEPENDENCIES FOUND'));
r=FDEP_dfind(p.magic,p,false,1);
disp(char(r.rm));
elseif ~par.opt.qflg
ntbx=numel(p.toolbox);
disp(sprintf('\n%9s: %-1d','toolboxes',ntbx));
for i=1:ntbx
disp(sprintf('%9s: %-1d %s',' ',i,p.toolbox{i}));
end
end
if nargout
po=p;
end
end
%-------------------------------------------------------------------------------
function [p,par]=FDEP_ini_engine(ver,magic,dopt,mopt,varargin)
% retrieve current FARG parameter templates
fap=farg;
F=false;
T=true;
p=[];
par=[];
% create output structure
p.magic=magic;
p.([magic,'ver'])=ver;
p.([fap.magic,'ver'])=fap.([fap.magic,'ver']);
p.MLver=version;
p.section_10='---------- ENGINE ------------';
p.rundate=datestr(clock);
p.runtime=[0,0,0];
p.par=[];
p.section_20='---------- INPUT ------------';
p.afile={};
p.file='';
p.section_30='---------- OUTPUT ------------';
p.ncall=0;
p.nfun=0;
p.module={};
p.fun={};
p.froot={};
p.sub={};
p.mix={}; % calls to
p.cix={}; % called from
p.mlix={}; % system calls
p.tix=[]; % function type
p.depth=[0,0];
p.nmlcall=zeros(1,6);
p.mlfun={};
p.toolbox={};
p.modbix={};
p.modbox=[];
p.tree={};
p.rtree={};
p.caller={};
p.mat=int8([]);
p.section_31='---------- macros -----------';
p.lib=[];
p.help=[];
p.get=[];
p.find=[];
p.list=[];
p.mhelp=[];
p.mplot=[];
p.tplot=[];
p.smod=[];
if isempty(varargin)
return;
end
% output parameters
% - description .TIX 0 1
%----------------------------------------------------------------
desc.t={
0 'C' 'caller no yes'
1 'R' 'recursive no yes'
2 'E' 'evals no yes'
3 'S' 'type script function'
4 'F' 'type M-file P-file'
};
% - description .SUB().n
%------------------------------------------------------------
desc.s={
1 'U' 'functions outside the scope'
2 'S' 'subfunctions'
3 'N' 'nested functions'
4 'A' 'anonymous functions'
5 'UD' 'user defined functions'
6 'MS' 'ML stock functions'
7 'MB' 'ML built-in functions'
8 'CE' 'calls to eval..'
9 'MC' 'ML classes'
10 'OC' 'other classes'
11 'TB' 'ML tbx'
12 'X' 'unresolved calls'
};
%
% - description .CALL{x,n}
%-------------------------------------------------------
desc.c={
1 'UD' 'user defined functions'
2 'MS' 'ML stock functions'
3 'MB' 'ML built-in functions'
4 'MC' 'ML classes'
5 'OC' 'other classes'
6 'TB' 'ML tbx'
};
% start engine
% - check file name
if ~ischar(varargin{1})
disp(sprintf('%s> input not a string: class <%s>',magic,class(varargin{1})));
return;
end
fnam=varargin{1};
% - select file name
% this is NOT as trivial as it seems...
[fpat,frot,fext]=fileparts(fnam); %#ok
if isempty(fext)
fext='.m';
end
afnam=which(fnam,'-all');
if isempty(afnam)
disp(sprintf('%s> file not found or not a valid MATLAB file <%s>',magic,varargin{1}));
return;
end
[apat,arot,aext]=cellfun(@fileparts,afnam,'uni',false); %#ok
ix= strcmp(arot,frot) &...
strcmp(aext,fext);
if ~any(ix)
disp(sprintf('%s> file not valid <%s>',magic,varargin{1}));
disp(sprintf('%s> files %8d\n',magic,numel(afnam)));
disp(char(afnam));
return;
else
fnam=afnam{find(ix,1,'first')};
end
if ~exist(fnam,'file')
disp(sprintf('%s> file not found <%s> = %s',magic,varargin{1},fnam));
return;
end
% - simple option parser
opt.qflg=false;
opt.lflg=false;
opt.mflg=false;
opt.dflg=false; % hidden flag
if nargin > 4
for i=1:numel(varargin)
switch varargin{i}
case '-q'
opt.qflg=true;
case '-l'
opt.lflg=true;
case '-m'
opt.mflg=true;
case '-d'
opt.dflg=true;
end
end
end
par.farg=fap;
par.opt=[];
par.desc=[];
par.enum=[];
% - macros
par.macro.sr=@(t) strrep(t,'\','/');
par.macro.range=@(x) max(x)-min(x); % replacement for <stats tbx>
% - engine parameters
[fpat,frot]=fileparts(fnam);
fnam=par.macro.sr(fnam);
par.mlroot=[matlabroot,filesep,'toolbox'];
par.opt=opt;
par.dopt=dopt;
par.mopt=mopt;
par.x=0; % loop counter
par.xx=1; % entry
par.c=0; % depth
par.e=0;
par.fix=[];
par.ac={sprintf('%-4d: * %s',0,frot)};
par.am={fnam};
par.nn=12;
par.nc=6;
par.call=cell(1,6);
% - templates
par.rexcls='^\w'; % module class
par.rexmod='\w+$'; % module
par.rexmod='(\w+$)|(\d+$)';
par.rextbx='\w+'; % find toolboxes
par.rexmbi='(?<=\().+(?=\))'; % full path to built-in
par.rexscr='(?<=\\)\w+(?=\.[mp]$)'; % file roots
par.spec=[0,0,0,0];
par.dspec='RESP';
par.fdes={'FUNCTION','SCRIPT'};
par.rdes={'no','yes','not known'};
par.edes={'not used','used','not known'};
% unique program id for the module manager
par.tagpid=sprintf('%s.%s.module.',magic,num2hex(rand));
par.tagid={
'PIDhelp > '
'PIDlist > '
'PIDmatrix > '
'PIDsynopsis > '
'PIDmanager > '
'PIDtree > '
};
par.tagid=strrep(par.tagid,'PID',par.tagpid);
par.tag={
@(varargin) sprintf('%s%s',par.tagid{1,1},varargin{1})
@(varargin) sprintf('%s%s',par.tagid{2,1},varargin{1})
@(varargin) sprintf('%s%s',par.tagid{3,1},varargin{1})
@(varargin) sprintf('%s%s',par.tagid{4,1},varargin{1})
@(varargin) sprintf('%s%s',par.tagid{5,1},varargin{1})
@(varargin) sprintf('%s%s',par.tagid{6,1},varargin{1})
};
par.ixbi=5; % MLINT calls
par.ixsub={...
% call M-file (ML)
'C',{2,T, 'ML M %s'}
% built-in
'B',{5,F, ' built-in'}
% call P-file (ML)
'P',{6,T, 'ML P %s'}
% call P-file (user)
'D',{7,T, 'CALL P %s'}
% recursion
'R',{-1,F, '------ RECURSION'}
% call M-file (user)
'M',{-5,T, 'CALL M %s'}
% subfunction
'S',{-100,F, '------ SUBFUNCTION'}
% nested
'N',{-101,F, '------ NESTED FUNCTION'}
% anonymous
'A',{-102,F, '------ ANONYMOUS FUNCTION'}
% f/eval...
'E',{-400,F, '------ EVAL'}
% unresolved
'X',{-500,F, '--**-- NOT FOUND'}
}.';
% descriptor: module
par.mtag={
'@@@UNRESOLVED@@@'
'@@@TOOLBOX@@@'
};
par.mktxtm=@(a,b,ix) {
sprintf('M-FILE : %s',a.sub(ix).M)
sprintf('P-FILE : %s',a.sub(ix).P)
sprintf('MODULE #%4d: %s',ix,a.module{ix})
sprintf('type : %s',b.fdes{a.tix(ix,4)+1})
sprintf('created : %s',b.date)
sprintf('size : %-1d bytes',b.size)
sprintf('lines : %-1d',a.sub(ix).l)
sprintf('comments : %-1d',a.sub(ix).cl)
sprintf('empty : %-1d',a.sub(ix).el)
sprintf('recursive : %s',b.rdes{a.tix(ix,2)+1})
sprintf('f/eval.. : %s',b.edes{a.tix(ix,3)+2*max([0,(a.tix(ix,5)-a.sub(ix).mp(1))])+1})
sprintf('unresolved : %s',b.rdes{double(a.sub(ix).n(b.enum.s.X)~=0)+isinf(a.sub(ix).n(b.enum.s.X))+1});
sprintf('calls TO : %5d user defined',numel(a.mix{ix}))
sprintf('called FROM: %5d user defined',numel(a.cix{ix}))
sprintf('calls in FILE: %5d ',a.sub(ix).n(b.enum.s.U))
sprintf('subfunctions : %5d inside file',a.sub(ix).n(b.enum.s.S))
sprintf('nested : %5d ',a.sub(ix).n(b.enum.s.N))
sprintf('anonymous : %5d ',a.sub(ix).n(b.enum.s.A))
sprintf('f/eval.. : %5d ',a.sub(ix).n(b.enum.s.CE))
sprintf('unresolved : %5d ',~isinf(a.sub(ix).n(b.enum.s.X))*a.sub(ix).n(b.enum.s.X));
par.mtag{1,1}
sprintf('ML stock: %5d ',numel(a.mlix{ix,1}))
sprintf('ML built-ins: %5d ',numel(a.mlix{ix,2}))
sprintf('ML classes: %5d ',numel(a.mlix{ix,3}))
sprintf('OTHER classes: %5d ',numel(a.mlix{ix,4}))
sprintf('ML toolboxes: %5d ',numel(a.mlix{ix,5}))
par.mtag{2,1}
};
% descriptor: main program
par.mktxtf=@(a,b,ix) {
sprintf('ROOT : %s',a.module{1})
sprintf('type : %s',b.fdes{a.tix(1,4)+1})
sprintf('recursive : %s',b.rdes{a.tix(1,2)+1})
sprintf('f/eval.. : %s',b.edes{a.tix(1,3)+2*max([0,(a.tix(1,5)-a.sub(1).mp(1))])+1})
sprintf('FDEP version: %s',a.FDEPver);
sprintf('ML version: %s',a.MLver)
sprintf('ML stock: %5d',a.nmlcall(1))
sprintf('ML built-ins: %5d',a.nmlcall(2))
sprintf('ML classes: %5d',a.nmlcall(3))
sprintf('OTHER classes: %5d',a.nmlcall(4))
sprintf('ML toolboxes: %5d',numel(a.mlfun{5}))
sprintf('runtime : %10.4f sec',sum(a.runtime(1)));
};
% MLINT templates
par.ftmpl='M0';
par.stmpl={
'U' 3 F @(x) regexp(x,par.rexmod,'match')
'S' 3 T @(x) regexp(x,par.rexmod,'match')
'N' 3 T @(x) regexp(x,par.rexmod,'match')
'A' [1,4] T ''
'E' 0 F ''
};
par.stmplf=fap.par.stmplf; % latest FARG
par.stmpla.M=[];
par.stmpla.P=[];
par.stmpla.mp=[];
par.stmpla.l=0;
par.stmpla.cl=0;
par.stmpla.el=0;
par.stmpla.nn=par.nn;
par.stmpla.des={};
par.stmpla.n=[];
for i=1:size(par.stmpl,1)
fn=par.stmpl{i,1};
par.stmpla.(fn)=par.stmplf{i,2};
end
% DEPFUN templates
par.mlfield={
'MLfunction'
'MLbuiltin'
'MLclass'
'OTHERclass'
'MLtoolbox'
'MLtoolbox' % KEEP THIS!
};
% link templates
par.sn={
% fieldname cell sort
'module' F F
'fun' F F
'sub' F F
'mix' T T
'cix' T T
'mlix' F F
'tix' F F
'depth' F F
};
par.ixsub=struct(par.ixsub{:});
par.call(1,1)={par.macro.sr(fnam)};
% - graphics
%{
original color scheme
par.mcol=[.75,1,1]; % color: modules
par.fcol=[1,1,.75]; % color: module
par.pcol=[.85,1,.85]; % color: main pg
%}
% john d'errico adjustment
par.mcol=[.85,1,1]; % color: modules
par.fcol=[1,1,.85]; % color: module
par.pcol=[.95,1,.95]; % color: main pg
par.tcol=[0,0,1]; % color: text
par.rcol=[1,.95,.95]; % color: tree
ss=get(0,'screensize');
par.lwin=[.3*ss(3),100,.7*ss(3)-20,ss(4)-160];
par.hwin=[10,50,.5*ss(3)-20,ss(4)-80];
% output structure
p.afile=afnam;
p.file=fnam;
% user defined functions
p.module={frot};
p.fun={fnam};
p.froot={[fpat,filesep,frot]};
p.sub=par.stmpla;
% descriptors/enumerators
fn=fieldnames(desc);
for i=1:numel(fn)
cf=fn{i};
par.desc.(cf)=desc.(cf);
par.enum.(cf)=desc.(cf)(:,[2,1]).';
par.enum.(cf)=struct(par.enum.(cf){:});
end
end
%-------------------------------------------------------------------------------
function [p,par]=FDEP_end_engine(p,par)
% stop engine
p.nfun=numel(p.fun);
p.par=par;
for i=1:p.nfun
p.fun=par.macro.sr(p.fun);
p.froot=par.macro.sr(p.froot);
end
ml=max(cellfun('length',par.ac));
cm=[num2cell(1:numel(par.ac));par.ac.';par.am.'];
fmt=sprintf('%%-4d %%-%ds -> %%s\n',ml+2);
p.tree=sprintf(fmt,cm{:});
p.tree=strread(p.tree,'%s','delimiter','','whitespace','');
txt=sprintf('%-9.9s: > %s','ROOT',p.file);
p.rtree=[[{txt},{''}];p.rtree];
p.rtree=[char(p.rtree(:,1)),repmat(' ',size(p.rtree,1),1),char(p.rtree(:,2))];
t=strrep(p.mlfun{1},par.macro.sr([matlabroot,filesep,'toolbox',filesep]),'');
p.mlfun{5}=unique(regexp(t,par.rextbx,'match','once'));
ntbx=numel(p.mlfun{5});
tf=true(ntbx,1);
tnam=cell(ntbx,1);
for i=1:ntbx
tver=ver(p.mlfun{5}{i});
if ~isempty(tver)
tnam{i}=sprintf('%s (%s) [%s]',tver.Name,tver.Version,p.mlfun{5}{i});
else
tf(i)=false;
end
end
p.mlfun{5}=p.mlfun{5}(tf);
if any(tf)
p.mlfun{6}=tnam(tf);
p.toolbox=tnam(tf);
else
p.mlfun{6}={};
p.toolbox=[];
end
p.cix=cell(p.nfun,1);
p.mlix=cell(p.nfun,6);
for i=1:p.nfun
ix=ismember(p.fun,par.call{i,1});
ix=par.fix(ix);
p.mix{i,1}=ix.';
p.cix(ix,1)=cellfun(@(x) [x,i],p.cix(ix,1),'uni',false);
for j=1:5
ix=ismember(p.mlfun{j},par.call{i,j+1});
p.mlix{i,j}=find(ix);
p.nmlcall(j)=numel(ix);
end
p.mlix{i,j+1}=p.mlix{i,j};
p.nmlcall(j+1)=p.nmlcall(j);
end
p=FDEP_fsort(p,par);
p=FDEP_cmp_depmat(p);
p=FDEP_parse_modules(p,par);
p=FDEP_get_modtbx(p);
end
%-------------------------------------------------------------------------------
function p=FDEP_flib(magic,p,nr)
% assign FDEP functions
% - do NOT change this tedious assingment!
% - requires a lot of time!
for i=1:nr
p.lib=@() FDEP_flib(magic,p,2);
p.help=@(varargin) FDEP_manager([],[],mfilename,p,'p',varargin{:});
p.get=@(varargin) FDEP_dget(magic,p,varargin{:});
p.find=@(varargin) FDEP_dfind(magic,p,true,varargin{:});
p.list=@(varargin) FDEP_dlist(magic,p,varargin{:});
p.mhelp=@(varargin) FDEP_mlist(magic,p,varargin{:});
p.mplot=@(varargin) FDEP_dplot(magic,p,varargin{:});
p.tplot=@(varargin) FDEP_rtree(magic,p,varargin{:});
if ~isfield(p,'smod')
p.smod=[];
end
end
end
%-------------------------------------------------------------------------------
function [p,par]=FDEP_get_dep(p,par,fnam)
par.x=par.x+1;
par.c=par.c+1;
fnam=which(fnam);
[fpat,frot]=fileparts(fnam); %#ok
[p,par,dtmp,dmlf,dmod,dmcl,docl]=FDEP_get_fun(p,par,fnam,frot);
wtmp=dtmp;
mltbx=strrep(dmlf,[matlabroot,filesep,'toolbox',filesep],'');
mltbx=unique(regexp(mltbx,par.rextbx,'match','once'));
par.call(par.xx,:)={
par.macro.sr(dtmp(2:end))
par.macro.sr(dmlf)
par.macro.sr(dmod)
par.macro.sr(dmcl)
par.macro.sr(docl)
par.macro.sr(mltbx)
};
if par.x > 1
p.mlfun={
unique([p.mlfun{1};par.call{par.xx,2}])
unique([p.mlfun{2};par.call{par.xx,3}])
unique([p.mlfun{3};par.call{par.xx,4}])
unique([p.mlfun{4};par.call{par.xx,5}])
unique([p.mlfun{5};par.call{par.xx,6}])
unique([p.mlfun{5};par.call{par.xx,6}])
};
else
p.mlfun=par.call(1,2:end).';
end
tmpd=cellfun(@(x) x(1:find(x=='.',1,'last')-1),...
dtmp,...
'uni',false);
ix=ismember(tmpd,p.froot);
dtmp(ix)=[];
ndtmp=numel(dtmp)-1;
p.ncall=p.ncall+ndtmp+1;
if numel(dtmp) < 2
m={'()'};
else
m=dtmp(2:end);
end
c=repmat({
sprintf('%-4d: %s| %s',...
par.x,repmat(' ',1,2*(par.c-1)),frot)
},...
numel(m),1);
par.ac=[par.ac;c];
par.am=[par.am;m];
p.tix(par.x,:)=[1,par.spec];
if ~numel(dtmp)
ix=ismember(p.fun,wtmp(2:end));
ofix='';
p.tix(par.x,1)=0;
if any(ix)
p.tix(par.x,1)=1;
ofix=par.fix(ix);
ofix=sprintf('%-1d ',ofix);
ofix(end)='';
end
fnum=numel(find(ix));
[p,par]=FDEP_show_entry(p,par,frot,fnum,ofix);
else
keep=par.c;
par.e=keep;
for i=1:numel(dtmp)
[dpat,drot]=fileparts(dtmp{i});
tmpd=[dpat,filesep,drot];
ix=ismember(tmpd,p.froot);
if ~any(ix)
p.fun=[p.fun;dtmp{i}];
p.froot=[p.froot;{tmpd}];
p.module=[p.module;{drot}];
if i == 1
[p,par]=FDEP_show_entry(p,par,frot,1,dtmp{i});
end
[p,par]=FDEP_get_dep(p,par,dtmp{i});
end
par.e=keep;
end
end
par.c=par.c-1;
end
%-------------------------------------------------------------------------------
function [p,par,dtmp,dmlf,dmod,dmcl,docl]=FDEP_get_fun(p,par,fnam,frot)
% DEPFUN arg description FDEP r2008a
% ------------------------------------
% 1 trace list dtmp
% 2 built-in list dmod
% 3 ML classes dmcl
% 4 problem list n/u
% 5 not used n/u
% 6 eval strings n/u
% 7 called from list docx
% 8 other classes docl
[dtmp,dmod,dmcl,docx,docx,docx,docx,docl]=depfun(fnam,par.dopt{:}); %#ok
im=strncmp(par.mlroot,dtmp,numel(par.mlroot));
dmlf=dtmp(im);
dtmp(im)=[];
% FARG
% - resolve calls inside the function file
[fa,fap]=farg(fnam,'-s','-d');
senum=par.enum.s;
tenum=par.enum.t;
% tix : CRESP
par.spec=[0,0,0,0]; % spec: RESP
dtmp{1}=fap.wnam;
if fap.mp(1)
p.fun{end}=fap.wnam;
dtmp{1}=fap.wnam;
elseif fap.mp(2)
p.fun{end}=fap.pnam;
dtmp{1}=fap.pnam;
else
p.fun{end}=fap.wnam;
end
fap.U=fap.UU; % reset to ALL U
ex=cellfun(@exist,fap.U.fn);
dmod=unique([dmod;fap.U.fn(ex==par.ixbi)]);
par.spec(tenum.F)=+fap.mp(2); % spec: M/P
sub=par.stmpla;
sub.M=par.macro.sr(fap.wnam); % - M-file name
sub.P=par.macro.sr(fap.pnam); % - P-file name
sub.mp=fap.mp; % - M/P indicator
sub.l=fap.par.nlen; % - #lines
sub.cl=fap.par.ncom; % - #comments
sub.el=fap.par.nemp; % - #empty lines
sub.des=par.desc.s(:,2).'; % - index descriptors
sub.n=zeros(1,sub.nn);
sub.n(1,size(par.stmpl,1):end)=...
[numel(dtmp)-1,numel(im),numel(dmod),0,numel(dmcl),numel(docl),0,0];
sub.E.fn={};
sub.E.bx=fap;
sub.E.ex=ex;
if isempty(fa)
if fap.par.mp(2)
sub.l=nan;
sub.n([senum.U,senum.CE])=nan; % - no MAIN
sub.n(senum.X)=inf;
end
if ~isempty(docx{1})
par.spec(tenum.R)=1; % spec: R
end
p.sub(par.xx,1)=sub;
return;
end
sub.n(senum.U:senum.A)=fap.n([6,2:4]);
if ~fap.par.nlen
par.spec(tenum.S)=1; % spec: S
dmlf={};
dmod={};
dmcl={};
docl={};
p.sub(par.xx,1)=sub;
return;
end
for i=1:size(par.stmpl,1)
if par.stmpl{i,2}
fn=par.stmpl{i,1};
sub.(fn)=fap.(fn);
end
end
sub.S.fn=fap.sub;
if fap.M.nx
sub.S.fd=[fap.M.fn;fap.S.fn];
sub.n(senum.S)=sub.n(senum.S)+1;
sub.S.nx=sub.S.nx+1;
sub.S.bx=[fap.M.bx,sub.S.bx];
end
c=fap.par.call;
sx=strmatch(par.stmpl{1,1},c);
c=c(sx);
f=regexp(c,par.rexmod,'match');
f=[f{:}].';
sub.E.fn=f;
sub.n(senum.X)=0;
sub.E.bx=fap;
sub.E.ex=ex;
% script
if ~fap.par.mfun
par.spec(tenum.S)=1; % spec: S
f=[frot;f];
m=regexp(dtmp,par.rexscr,'match');
m=[m{:}].';
is=ismember(m,f);
dtmp=dtmp(is);
end
[sub,par]=FDEP_parse_calls(frot,sub,par);
p.sub(par.xx,1)=sub;
end
%-------------------------------------------------------------------------------
function [sub,par]=FDEP_parse_calls(module,sub,par)
% resolve calls
% - subroutines
f=sub.E.fn;
ex=sub.E.ex;
fap=sub.E.bx;
senum=par.enum.s;
tenum=par.enum.t;
% f/eval..
ir= strncmp('eval',fap.par.ltok(:,2),4) |...
strncmp('feval',fap.par.ltok(:,2),5);
if any(ir)
par.spec(tenum.E)=1; % spec: E
sub.n(senum.CE)=numel(find(ir));
ir= strncmp('eval',fap.U.fn,4) |...
strncmp('evalc',fap.U.fn,5) |...
strncmp('evalin',fap.U.fn,6) |...
strncmp('feval',fap.U.fn,5);
ex(ir)=par.ixsub(1).E;
end
% resolved
fu=sub.U.fn;
if sub.n(senum.S)
ir=ismember(fu,sub.S.fd);
ex(ir)=par.ixsub(1).S;
end
if sub.n(senum.N)
ir=ismember(fu,sub.N.fn);
ex(ir)=par.ixsub(1).N;
end
% unresolved
fx=find(ex==0);
sub.E.fn=fap.U.fn(~ex);
ns=numel(sub.S.fd);
ne=numel(sub.E.fn);
if all([ns,ne]) &&...
any(fx)
fu=sub.E.fn;
if sub.n(senum.S)
ir=ismember(fu,sub.S.fd);
ex(fx(ir))=par.ixsub(1).S;
end
if sub.n(senum.N)
ir=ismember(fu,sub.N.fn);
ex(fx(ir))=par.ixsub(1).N;
end
end
% recursion
ir=strcmp(module,f);
if any(ir)
par.spec(1)=1; % spec: R
ex(ir)=par.ixsub(1).R;
end
sub.n(senum.X)=sum(~ex);
sub.E.fn=fap.U.fn(~ex);
ex(~ex)=par.ixsub(1).X;
sub.E.ex=ex;
if sub.U.nx
sub.E.bx=sub.U.bx(1,ex==par.ixsub(1).X);
else
sub.E.bx=[];
end
end
%-------------------------------------------------------------------------------
function [p,par]=FDEP_parse_modules(p,par)
% resolve calls
% - external modules
p.fun=par.macro.sr(p.fun);
p.froot=par.macro.sr(p.froot);
for i=1:p.nfun
sub=p.sub(i,1);
if ~isempty(sub.E.ex)
ex=sub.E.ex;
sub.U.fd=repmat({''},size(sub.U.fn));
% P-files
ir=find(ex==par.ixsub(1).P);
if any(ir)
sub.U.fd(ir)=cellfun(@(x) par.macro.sr(which(x)),...
sub.U.fn(ir),...
'uni',false);
a=ismember(sub.U.fn(ir),p.module);
ex(ir(a))=par.ixsub(1).D;
end
% user-defined functions
ir=find(ex==par.ixsub(1).C);
if any(ir)
[a,b]=ismember(sub.U.fn(ir),p.module);
if any(a)
sub.U.fd(ir(a))=p.fun(b(b>0));
ex(ir(a))=par.ixsub(1).M;
end
% ML stock functions
ir=ir(~a);
if any(ir)
sub.U.fd(ir)=cellfun(@(x) par.macro.sr(which(x)),...
sub.U.fn(ir),...
'uni',false);
end
end
sub.E.ex=ex;
end
p.sub(i,1)=sub;
end
end
%-------------------------------------------------------------------------------
function p=FDEP_cmp_depmat(p)
tix=(p.tix(:,1)~=0 | p.tix(:,2)~=0).';
p.caller=p.module(tix); % functions
nc=(1:sum(tix)).';
p.mat=zeros(p.nfun,'int8');
for i=1:p.nfun
p.mat(p.mix{i},i)=1;
if p.tix(i,2)