Code covered by the BSD License  

Highlights from
Pcode Release Utility

image thumbnail

Pcode Release Utility

by

 

Create a one pcode file for the main function and the called functions together.

release_package_pcode(function_name)
function release_package_pcode(function_name)
% This function will package "function_name" and its subfunctions together
% into one pcode "function_name.p" and also creates a help file(*.m) with
% just the user comments added in "function_name.m".
%
% Syntax: >>release_package_pcode(<function_name>)
% <function_name> - Name of the main function that has to be distributed as
% pcode.
% Output: release_files_<date>, ready to release/distribute folder.
%
% Here are activities done by this utility.
%   - Reads all the subfunctions of "<function_name>".
%   - Creates a new file with the name "<function_name>", and copies the main
%   function and the all the subfunctions into this.
%   - Converts the new file as pfile "<function_name>.p".
%   - Lists and copies all the "other files" pcodes, mex files and others
%   that are called by <function_name>.m to the user.
%   - Copies the help comments from the "<function_name>" and prints the help
%   comments to a file "<function_name>.m"
%
% Now the "<function_name>.p" and "<function_name>.m" are the distributable to
% the other users along with the "other files".
%
% All files are placed in directory release_files_<date>. If the directory
% already available, then it overwrites it.
%
% Constraint: All functions should use either one of the closing command
% "return" or "end" uniformly between them. Else the function will stop the
% pcode conversion.
%
% Developed by: Neelakanda Bharathiraja.
%

try
    % Find the dependant functions in the tools directory.
    trace_list = depfun(function_name,'-quiet');
catch
    disp(['The function ' function_name ' is not available in the MATLAB path!']);
    return;
end

% Remove the functions that are from matlabroot
trace_list(strncmp(matlabroot,trace_list,length(matlabroot)))=[];

% Group the m-files and other files.
function_files = {};
other_files = {};
for ii = 1:length(trace_list)
    [file_dir, name, ext]= fileparts(trace_list{ii});
    if strcmp(ext,'.m')
        function_files = [function_files trace_list(ii)];
    else
        other_files = [other_files trace_list(ii)];
    end
end

% Copy the contents of all the mfiles and write it into the <function_name>.m
release_dir = [function_name '_release_' regexprep(datestr(clock),'[-\s:]','_')];
mkdir(release_dir);
cd(release_dir);
fileID = fopen([function_name '.m'], 'w+');
for ii = 1:length(function_files)
    fid = fopen(function_files{ii});
    tline = fgets(fid);
    while ischar(tline)
        fwrite(fileID, tline);
        tline = fgets(fid);
    end
    fclose(fid);
    fwrite(fileID, char(10));
end
fclose(fileID);

% Get the help commands.
help_commands = help(function_name);

try
    % Create the pcode.
    pcode([release_dir '\' function_name '.m']);
catch
    disp(['Pcode generation process is not successful.'  char(10) ...
        'Check the file ' release_dir '\' function_name '.m']);
    return;
end

% Print the dependant files.
if ~isempty(other_files)
    disp('List of files that are moved to the release and might necessary to distribute:');
    for ii = 1:length(other_files)
        disp(other_files{ii});
        try
            copyfile(other_files{ii},release_dir)
        catch
            disp(['Failed to move the file: ' other_files{ii}]);
        end
    end
end

% Print the help document.
fileID = fopen([function_name '.m'], 'w+');
edit_commands = ['%' regexprep(help_commands,char(10),[char(10) '%'])];
fwrite(fileID, edit_commands);
fclose(fileID);

return;

Contact us