通过MATLAB编译函数运行.m文件 [英] Running an .m file from a MATLAB-compiled function

查看:366
本文介绍了通过MATLAB编译函数运行.m文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在MATLAB中编译以下简单函数

Say I compile the following simple function in MATLAB

function foo(path_to_m_file)
  disp([' Running ' path_to_m_file])
  run(path_to_m_file);
end

函数foo仅采用 .m 文件的路径并尝试运行它.

The function foo just takes a path to an .m file and tries to run it.

但是,当我实际尝试在编译后运行foo时:

However, when I actually try to run foo after compiling it:

./run_foo.sh $path_to_run_time $path_to_m_file

其中path_to_m_file是一个简单的 .m 文件,带有以下语句:

where path_to_m_file is a simple .m file with a statement such as:

a = 2;

我收到以下错误:

Error using ==> run
MATLAB:run:FileNotFound

但是,我知道foo获得了正确路径.例如,如果我尝试用foo

However, I know that foo gets the correct path. For example, if I try replacing the line with run by the following two lines in foo

fID = fopen(conf_file, 'rt');
first_line = textscan(fID, '%s', Inf, 'Delimiter', '\n');

foo读取.m文件的相应行.因此.m文件就在其中,MATLAB引擎可以看到"它.确实,我什至可以在用textscan读取的字符串上运行eval.

foo reads the corresponding line of the .m file. So the .m file is there, and the MATLAB engine can "see" it. Indeed I can even run eval on strings read with textscan.

  1. 为什么会出现上述错误?为什么foo不运行.m文件?

更新:有关此问题的答案,请参见下面的@ strictlyrude27答案.

Update: See @strictlyrude27's answer below for what seems to be an answer to this question.

第二个问题的动机:

我希望能够更新"项目中的.m文件,而不必重新编译整个项目.任何想法,将不胜感激.

The motivation for my second question:

I would like to have the ability to "update" an .m file that is part of the project without having to re-compile the full project. Any ideas for this would be greatly appreciated.

推荐答案

从MATLAB编译器的文档:

From the MATLAB Compiler's documentaton:

编译后的应用程序在运行时不处理MATLAB文件

设计了MATLAB编译器,以便您可以部署锁定的功能.在MATLAB Compiler加密它们时,可部署的MATLAB文件将被挂起或冻结-从那时起,它们不会发生变化.这并不意味着您不能部署灵活的应用程序,而是意味着您在设计应用程序时必须牢记灵活性.例如,如果您希望最终用户能够在两种不同的方法之间进行选择,则必须同时编译这两种方法.

The MATLAB Compiler was designed so that you can deploy locked down functionality. Deployable MATLAB files are suspended or frozen at the time MATLAB Compiler encrypts them—they do not change from that point onward. This does not mean that you cannot deploy a flexible application—it means that you must design your application with flexibility in mind. If you want the end user to be able to choose between two different methods, for example, they both must be compiled in.

MCR仅适用于在构建组件时已加密的MATLAB代码.动态生成新MATLAB代码的任何函数或过程均不适用于MCR.

The MCR only works on MATLAB code that was encrypted when the component was built. Any function or process that dynamically generates new MATLAB code will not work against the MCR.

某些MATLAB工具箱,例如Neural Network Toolbox™产品,会动态生成MATLAB代码.由于MCR仅执行加密的MATLAB文件,而神经网络工具箱生成未加密的MATLAB文件,因此无法部署神经网络工具箱中的某些功能.

Some MATLAB toolboxes, such as the Neural Network Toolbox™ product, generate MATLAB code dynamically. Because the MCR only executes encrypted MATLAB files, and the Neural Network Toolbox generates unencrypted MATLAB files, some functions in the Neural Network Toolbox cannot be deployed.

类似地,无法部署需要检查MATLAB函数文件内容的函数.例如,HELP是动态的,在部署模式下不可用.如果为LOADLIBRARY提供了MATLAB函数原型,则可以在部署模式下使用.

Similarly, functions that need to examine the contents of a MATLAB function file cannot be deployed. HELP, for example, is dynamic and not available in deployed mode. You can use LOADLIBRARY in deployed mode if you provide it with a MATLAB function prototype.

执行以下任务,而不是编译生成MATLAB代码的函数并尝试对其进行部署:

Instead of compiling the function that generates the MATLAB code and attempting to deploy it, perform the following tasks:

  1. 在MATLAB中运行一次代码,以获取生成的函数.

  1. Run the code once in MATLAB to obtain your generated function.

使用MATLAB Compiler编译MATLAB代码,包括生成的函数.

Compile the MATLAB code with MATLAB Compiler, including the generated function.

提示:使用EVALFEVAL的另一种方法是使用匿名函数句柄. 如果您需要能够创建用于动态运行时处理的MATLAB代码,则最终用户必须具有已安装的MATLAB副本.

Tip: Another alternative to using EVAL or FEVAL is using anonymous function handles. If you require the ability to create MATLAB code for dynamic run time processing, your end users must have an installed copy of MATLAB.

这篇关于通过MATLAB编译函数运行.m文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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