Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Iterating MATLAB Commands
- % from https://nickhigham.wordpress.com/2016/05/13/iterating-matlab-commands/
- % Some MATLAB commands can be ?applied to themselves?. A good example is
- % help help, which provides help on the help function. For what other
- % functions fun is fun fun a legal statement that produces something
- % interesting? Here are some examples (by no means an exhaustive list).
- %
- % char char
- % demo demo
- % diary diary
- % doc doc
- % diff diff
- % double double
- % edit edit
- % error error
- % iskeyword iskeyword
- % length length
- % magic magic
- % max max
- % size size
- % sort sort
- % spy spy
- % unique unique
- % upper upper
- % warning warning
- % which which
- %
- % Can you see why these are legal and guess what the effect of these calls
- % will be? Search for ?command-function duality? in the MATLAB help for
- % some clues.
- %
- % Are there any legal triple invocations? Yes, for example
- %
- % and and and
- % char char char
- % isa isa isa
- % max max max
- % Indeed char can be iterated as many times as you like, but and and isa
- % can be iterated three times but not twice.
- %
- % If you think of additional interesting examples, please add them to the
- % comments below.
- %% grab the folders and files on the path:
- restoredefaultpath
- [~,~,~,~,~,~,folders] = regexp( path , ':' );
- folders = setdiff( folders , pwd );
- files = {};
- for f = 1:numel(folders)
- files = cat( 1 , files , dirRecurse( folders{f} ) );
- end
- files = unique( files );
- % we know these work, but they break the rest of our search!
- dangerous = { ...
- 'cell.m' , ... size(cell cell cell) is 99 x 99,
- ... size(cell cell cell cell) is 99 x 99 x 99
- ... and so forth...dangerous when output isn't suppressed
- 'depdir.m', ... at 5 and on takes considerable time
- 'depfun.m', ... at 5 and on takes considerable time
- 'matlabpath.m' , ... breaks the path
- 'memmap_data_handle_holder.m' , ... causes a segfault in 'menu menu'???
- 'path.m' , ... breaks the path
- 'spinmap.m', ... first argument is the duration of a while loop
- 'rjr.m' ... second argument is ignored and gets us caught in a while loop
- };
- files = setdiff( files , dangerous )';
- F = numel(files);
- %% check which can be run how many times
- N = 10;
- invocations = false(F,N);
- exceptions = cell(F,N);
- for n = 2:N
- for f = 1:F
- fun = files{f}(1:end-2);
- command = [ repmat([fun ' '],1,n-1) fun ];
- fprintf( 'Trying %s\n' , command );
- try
- eval( sprintf('%s' , command ) );
- invocations(f,n) = true;
- catch ME
- exceptions{f,n} = ME;
- end
- end
- end
- save iterating_MATLAB_commands.mat files dangerous invocations exceptions
- close all
- %% did we find all of Nich Higham's?
- highamTwice = cellfun( @(s) [s '.m'] , { ...
- 'char', 'demo', 'diary', 'doc', 'diff', 'double', 'edit', ...
- 'error', 'iskeyword', 'length', 'magic', 'max' ,'size' ,'sort' , ...
- 'spy' ,'unique' ,'upper' ,'warning', 'which'} , ...
- 'UniformOutput', false);
- highamTwice( ~ismember( highamTwice , files(invocations(:,2) ) ) )
- %% find the interesting ones
- % how many can be invoked for how long?
- clf
- plot(2:10, sum(invocations(:,2:end),1),'.-b','Linewidth',4,'Markersize',30)
- hold on
- % how many can uniquely be invoked that long?
- maxInvocations = invocations;
- for f = 1:F
- idx = find( invocations(f,:) , 1, 'last' );
- maxInvocations(f,1:idx-1) = false;
- end
- plot(2:10, sum(maxInvocations(:,2:end),1),'.-r','Linewidth',4,'Markersize',30)
- set(gca,'FontSize',14,'FontName','Monospaced')
- xlabel('Self-Invocation Count')
- ylabel('Function Count')
- legend('Invocations of Functions','Maximum acheived invocations')
- print(gcf,'-dpng','invocations.png');
- %% so what are those cool ones?
- % !!! MATLAB does not have nargin/nargout information for ...
- narginIssues = {'colstyle.m','enumeration.m','events.m','gcf.m', ...
- 'libisloaded.m','loadobj.m','metaclass.m','properties.m', ...
- 'saveobj.m','superclasses.m','ishghandle.m','ancestor.m','findobj.m'};
- for n = 1:N
- fprintf( '\n---------- %i ----------\n', n );
- these = files( maxInvocations(:,n) );
- these = setdiff( these , narginIssues )';
- 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);
- disp(toPrint)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement