Matlab导入功能的范围是什么? [英] What is the scope of Matlab's import function?

查看:137
本文介绍了Matlab导入功能的范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用Matlab编写的一些代码转换成一个独立的,已编译的Matlab应用程序.但是,在遇到一些奇怪的错误之后,我意识到代码大量使用了从路径添加和删除的方法,从而避免了多次使用相同名称(但结果/计算结果不同)的多个函数的事实.环顾四周,我发现您可以通过以下方式将文件夹变成一个包:在文件夹名称前加上"+",然后使用name_of_folder.name_of_function确保该包中的功能相互引用.这解决了名称空间的问题,但可能会产生大量工作,因为我现在必须仔细检查并在每个函数调用之前添加正确的包(而且我可能仍然不得不复制很多文件).

然后,我看到了import函数,希望可以为我节省一些时间.我想我可以将我想要的包传递给一个或两个特定的函数,让那些函数导入该包,然后事情将按照我想要的方式工作-如果这些函数调用的函数属于该import语句的范围.例如,如果我设置了类似的内容

function foo(var1, var2, ..., packagename)
  eval(sprintf('import %s.*', packagename));
  ...
  bar1(var1, var2);
  ...
  bar2(var2);
  ...

然后我希望bar1bar2将使用与import语句一起导入的包.该文档说,条件和函数中的import语句仅限于该代码块,但是我不知道那个代码块"是仅表示文本,还是那个代码块"是代码以及所有评估结果因此.我感觉是前者,但我想问一下,希望是后者.

那么,导入语句的范围是什么?另外,还有其他方法可以解决此问题吗?

解决方案

我编写了一些测试代码来亲自尝试一下,确实,import语句仅限于调用它的函数,这很有意义,但我猜猜我的希望蒙蔽了我的判断力.为了记录,我编写了以下简短函数对其进行测试:

function package_test(package_name)
  eval(sprintf('import %s.*;', package_name));

  test_function();
end

 

function test_function()
  nested_function()
end

然后放

function nested_function()
  disp('I\'m in the original folder :(');
end

与前两个函数位于同一文件夹中,并且

function nested_function()
  disp('I\'m in the package! :)');
end

在名为+trial的文件夹中.当然,当我运行package_test('trial')时,我看到窗口中显示了我在原始文件夹:("中,而trial.nested_function()给了我希望看到的字符串.

此外,eval函数给编译器带来了问题,用import(sprintf('%s.*', package_name));替换它似乎也无济于事.因此,看来我又要复制文件了.

I'm trying to turn some code written in Matlab into a standalone, compiled Matlab application. However, after getting some odd errors, I realized that the code makes a lot of use of adding and removing from the path to get around the fact that there are several functions with the same name (but different results/calculations) used multiple times. Looking around, I discovered that you can turn a folder into a package by putting a "+" in front of its name, and going through and making sure the functions in that package refer to each other using name_of_folder.name_of_function. This solves the namespace problem, but it potentially creates a lot of work, in that I now have to go through and prepend the correct package to each function call (and I may still end up having to duplicate a lot of files).

Then I saw the import function, and I'm hoping that'll save me some time. I think I can pass the package I want to one or two particular functions, have those function import the package, and then things will work the way I want--if the functions that those functions call fall into the scope of that import statement. E.g, if I set something up like

function foo(var1, var2, ..., packagename)
  eval(sprintf('import %s.*', packagename));
  ...
  bar1(var1, var2);
  ...
  bar2(var2);
  ...

then I'm hoping bar1 and bar2 will use the package imported with the import statement. The documentation says that import statements in conditionals and functions are limited to that block of code, but I don't know if "that block of code" means that text only, or "that block of code" being the code and everything that evaluates as a result. I have a feeling it's the former, but I figured I'd ask in the hopes that it is the latter.

So, what is the scope of an import statement? Alternatively, is there another way to deal with this problem?

解决方案

I wrote some test code to try it out for myself, and indeed, the import statement is limited to only the function that called it, which makes sense but I guess my hope clouded my judgement. For the record, I wrote the following short functions to test it:

function package_test(package_name)
  eval(sprintf('import %s.*;', package_name));

  test_function();
end

 

function test_function()
  nested_function()
end

and then put

function nested_function()
  disp('I\'m in the original folder :(');
end

in the same folder as the first two functions, and

function nested_function()
  disp('I\'m in the package! :)');
end

in a folder named +trial. Of course, when I ran package_test('trial'), I saw "I'm in the original folder :(" displayed in the window, while trial.nested_function() gave me the string I hoped to see.

Furthermore, the eval function poses a problem to the compiler, and replacing it with import(sprintf('%s.*', package_name)); doesn't seem to help either. So it looks like I'm back to duplicating files.

这篇关于Matlab导入功能的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆