Code covered by the BSD License  

Highlights from
jEdit Utilities

jEdit Utilities

by

 

03 Oct 2013 (Updated )

A collection of utilities to facilitate using jEdit text editor with MATLAB.

writeFunctionList(func_file)
function [] = writeFunctionList(func_file)

% writeFunctionList - Writes to file a sorted list of MATLAB functions for all
% MathWorks products installed on the machine (that the function is executed
% on). Denotes which functions are built-in to help with syntax highlighting.
%
% [] = writeFunctionList(func_file)
% func_file = function name text filename

path_str = path;

% Break apart path and determine candidate function names.
end_ind = strfind(path_str, ';') - 1;
start_ind = [1 end_ind(1:end-1)+2];

k = 1;
for i = 1 : length(start_ind)
  dir_str = path_str(start_ind(i):end_ind(i));

  if strfind(dir_str, matlabroot) % only looks at directories underneath root directory
    if isempty(regexp(dir_str, 'demo', 'once')) % exclude demo directories
      S = dir(dir_str);

      for j = 1 : length(S)
        if regexp(S(j).name, '[Cc]ontents.m')
          continue;
        end

        if regexp(S(j).name, '(\.m\>)') % match .m at end
          func_name{k} = S(j).name(1:end-2);
          k = k + 1;
        end
      end
    end
  end
end

% Screen function names and write to file.
func_name = unique(func_name);
fid = fopen(func_file, 'wt');

for i = 1 : length(func_name)
  if exist(func_name{i}, 'builtin') % built-in function
    fprintf(fid, '%s\tbuilt-in\n', func_name{i});
  else
    if is_a_script(func_name{i}) == 0 % function
      fprintf(fid, '%s\n', func_name{i});
    else % script or other
      continue;
    end
  end
end

fclose(fid);


function is_script = is_a_script( varargin )

% Copied from: http://stackoverflow.com/questions/15910210/distinguish-between-scripts-and-functions-programmatically
% See also: http://blogs.mathworks.com/loren/2013/08/26/what-kind-of-matlab-file-is-this
% is_a_script( varargin ) returns one of the following:
%   1: if the input is a script
%   0: if the input is a function
%  -1: if the input is neither a function nor a script.

is_script = 0;
switch( exist(varargin{1}) )
    case 2
        % If the input is not a MEX or DLL or MDL or build-in or P-file or variable or class or folder,
        % then exist() returns 2
        try
            nargin(varargin{1});
        catch err
            % If nargin throws an error and the error message does not match the specific one for script, then the input is neither script nor function.
            if( strcmp( err.message, sprintf('%s is a script.',varargin{1}) ) )
                is_script = 1;
            else
                is_script = -1;
            end
        end
    case {3, 4, 5, 6} % MEX or DLL-file, MDL-file, Built-in, P-file
        % I am not familiar with DLL-file/MDL-file/P-file. I assume they are all considered as functions.
        is_script = 0;
    otherwise % Variable, Folder, Class, or other cases 
        is_script = -1;
end

Contact us