Skip to content

Instantly share code, notes, and snippets.

@abravalheri
Forked from jaeandersson/import.m
Created January 19, 2017 15:10
Show Gist options
  • Save abravalheri/ed5e8a85d3ca6ab172ca41c85c9ce44f to your computer and use it in GitHub Desktop.
Save abravalheri/ed5e8a85d3ca6ab172ca41c85c9ce44f to your computer and use it in GitHub Desktop.
Import a package into Octave using the syntax "import packagename.*"
function import(varargin)
% Import a package. Only entire packages can be imported currently.
error(nargchk(1, inf, nargin, 'struct'));
% Import the packages one-by-one
for i=1:nargin
import1(varargin{i});
end
end
function import1(pkgname)
% Split up name into parts
pkgname_parts = strsplit(pkgname, '.');
% Fallback to system import command if not entire package (i.e. failure)
if length(pkgname_parts)~= 2 || ~strcmp(pkgname_parts{end}, '*')
error('Only the syntax ''import package_name.*'' is currently supported')
end
% Get path for package
pkgpath = locatepkg(pkgname_parts{1});
% Add to path
addpath(pkgpath);
end
function pkgpath = locatepkg(pkgname)
pathdirs = strsplit(path, pathsep);
for iPath=1:length(pathdirs)
pkgpath = [pathdirs{iPath} filesep '+' pkgname];
if exist(pkgpath, 'dir')
return;
end
end
error('Package ''%s'' cannot be located in the path', pkgname);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment