SHARE
TWEET

iterating_MATLAB_commands.m

davidagross Jun 10th, 2016 (edited) 158 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % Iterating MATLAB Commands
  2. % from https://nickhigham.wordpress.com/2016/05/13/iterating-matlab-commands/
  3.  
  4. % Some MATLAB commands can be ?applied to themselves?. A good example is
  5. % help help, which provides help on the help function. For what other
  6. % functions fun is fun fun a legal statement that produces something
  7. % interesting? Here are some examples (by no means an exhaustive list).
  8. %
  9. % char char
  10. % demo demo
  11. % diary diary
  12. % doc doc
  13. % diff diff
  14. % double double
  15. % edit edit
  16. % error error
  17. % iskeyword iskeyword
  18. % length length
  19. % magic magic
  20. % max max
  21. % size size
  22. % sort sort
  23. % spy spy
  24. % unique unique
  25. % upper upper
  26. % warning warning
  27. % which which
  28. %
  29. % Can you see why these are legal and guess what the effect of these calls
  30. % will be? Search for ?command-function duality? in the MATLAB help for
  31. % some clues.
  32. %
  33. % Are there any legal triple invocations? Yes, for example
  34. %
  35. % and and and
  36. % char char char
  37. % isa isa isa
  38. % max max max
  39. % Indeed char can be iterated as many times as you like, but and and isa
  40. % can be iterated three times but not twice.
  41. %
  42. % If you think of additional interesting examples, please add them to the
  43. % comments below.
  44.  
  45. %% grab the folders and files on the path:
  46.  
  47. restoredefaultpath
  48. [~,~,~,~,~,~,folders] = regexp( path , ':' );
  49. folders = setdiff( folders , pwd );
  50. files = {};
  51. for f = 1:numel(folders)
  52.     files = cat( 1 , files , dirRecurse( folders{f} ) );
  53. end
  54. files = unique( files );
  55. % we know these work, but they break the rest of our search!
  56. dangerous = { ...
  57.     'cell.m' , ...                      size(cell cell cell) is 99 x 99,
  58.     ...                                 size(cell cell cell cell) is 99 x 99 x 99
  59.     ...                                 and so forth...dangerous when output isn't suppressed
  60.     'depdir.m', ...                     at 5 and on takes considerable time
  61.     'depfun.m', ...                     at 5 and on takes considerable time
  62.     'matlabpath.m' , ...                breaks the path
  63.     'memmap_data_handle_holder.m' , ... causes a segfault in 'menu menu'???
  64.     'path.m' , ...                      breaks the path
  65.     'spinmap.m', ...                    first argument is the duration of a while loop
  66.     'rjr.m' ...                         second argument is ignored and gets us caught in a while loop
  67.     };
  68. files = setdiff( files , dangerous )';
  69. F = numel(files);
  70.  
  71. %% check which can be run how many times
  72.  
  73. N = 10;
  74. invocations = false(F,N);
  75. exceptions = cell(F,N);
  76. for n = 2:N
  77.     for f = 1:F
  78.         fun = files{f}(1:end-2);
  79.         command = [ repmat([fun ' '],1,n-1) fun ];
  80.         fprintf( 'Trying %s\n' , command );
  81.         try
  82.             eval( sprintf('%s' , command ) );
  83.             invocations(f,n) = true;
  84.         catch ME
  85.             exceptions{f,n} = ME;
  86.         end
  87.     end
  88. end
  89. save iterating_MATLAB_commands.mat files dangerous invocations exceptions
  90. close all
  91.  
  92. %% did we find all of Nich Higham's?
  93.  
  94. highamTwice = cellfun( @(s) [s '.m'] , { ...
  95.     'char', 'demo', 'diary', 'doc', 'diff', 'double', 'edit', ...
  96.     'error', 'iskeyword', 'length', 'magic', 'max' ,'size' ,'sort' , ...
  97.     'spy' ,'unique' ,'upper' ,'warning', 'which'} , ...
  98.     'UniformOutput', false);
  99. highamTwice( ~ismember( highamTwice , files(invocations(:,2) ) ) )
  100.  
  101. %% find the interesting ones
  102.  
  103. % how many can be invoked for how long?
  104. clf
  105. plot(2:10, sum(invocations(:,2:end),1),'.-b','Linewidth',4,'Markersize',30)
  106. hold on
  107.  
  108. % how many can uniquely be invoked that long?
  109. maxInvocations = invocations;
  110. for f = 1:F
  111.     idx = find( invocations(f,:) , 1, 'last' );
  112.     maxInvocations(f,1:idx-1) = false;
  113. end
  114. plot(2:10, sum(maxInvocations(:,2:end),1),'.-r','Linewidth',4,'Markersize',30)
  115.  
  116. set(gca,'FontSize',14,'FontName','Monospaced')
  117. xlabel('Self-Invocation Count')
  118. ylabel('Function Count')
  119. legend('Invocations of Functions','Maximum acheived invocations')
  120. print(gcf,'-dpng','invocations.png');
  121.  
  122. %% so what are those cool ones?
  123.  
  124. % !!! MATLAB does not have nargin/nargout information for ...
  125. narginIssues = {'colstyle.m','enumeration.m','events.m','gcf.m', ...
  126.     'libisloaded.m','loadobj.m','metaclass.m','properties.m', ...
  127.     'saveobj.m','superclasses.m','ishghandle.m','ancestor.m','findobj.m'};
  128. for n = 1:N
  129.     fprintf( '\n---------- %i ----------\n', n );
  130.     these = files( maxInvocations(:,n) );
  131.     these = setdiff( these , narginIssues )';
  132.     toPrint = cellfun( @(s) sprintf('% 2i args - %s',nargin(s(1:end-2)), [ repmat([s(1:end-2) ' '],1,n-1) s(1:end-2) ]), these , 'UniformOutput' , false);
  133.     disp(toPrint)
  134. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top