在Octave中导入函数

9
我正在Octave中运行Matlab代码。我猜核心Octave中没有实现import函数。有任何想法如何在Octave中使用这个Matlab函数吗?
以下是我的代码: octave-3.4.0:7> setup 导入包: brml.* 警告:`import'函数在Octave中尚未实现
请阅读 `http://www.octave.org/missing.html' 以了解如何贡献缺失的功能。
错误:第8行第5列附近未定义`import'

1
你能否发布一下你使用 import 函数的代码? - am304
1个回答

6

您可以创建自己的自定义import.m文件。来源于http://octave.1599824.n4.nabble.com/namespace-support-td1638758.html:

function import(varargin) 
 error(nargchk(1, inf, nargin, "struct")); 
 for i=1:nargin 
   [names, funcs] = import1(varargin{i}); 
   for j=1:length(names) 
     assignin("caller", names{j}, funcs{j}); 
   endfor 
 endfor 
endfunction 

function [names, funcs] = import1(pkgname) 
 pkgname_parts = strsplit(pkgname, "."); 
 if length(pkgname_parts) > 2 
   error("invalid package name: %s", pkgname); 
 endif 
 pkgpath = locatepkg(pkgname_parts{1}); 
 unwind_protect 
   cwd = pwd; 
   cd(pkgpath); 
   names = what(pwd); 
   names = {names.m{:}, names.mex{:}, names.oct{:}}; 
   names = cellfun(@stripExtension, names, "UniformOutput", false); 
   if length(pkgname_parts) == 2 
     if any(strcmp(pkgname_parts{2}, names)) 
       names = {pkgname_parts{2}}; 
     else 
       error("function `%s' not found in package `%s'", ... 
         pkgname_parts{2}, pkgname_parts{1}); 
     endif 
   endif 
   funcs = cellfun(@str2func, names, "UniformOutput", false); 
 unwind_protect_cleanup 
   cd(cwd); 
 end_unwind_protect 
endfunction 

function pkgpath = locatepkg(pkgname) 
 pathdirs = strsplit(path, pathsep); 
 for iPath=1:length(pathdirs) 
   pkgpath = [pathdirs{iPath} filesep "+" pkgname]; 
   if exist(pkgpath, "dir") 
     return; 
   endif 
 endfor 
 error("package `%s' cannot be located in the path", pkgname); 
endfunction 

function fileName = stripExtension(fileName) 
 dotIndices = strfind(fileName, "."); 
 fileName = fileName(1:(dotIndices(end)-1)); 
endfunction 

是否可以从Scilab / ScosLab导入函数?我正在尝试解决这个问题 - Foad S. Farimani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接