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

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

问题描述

背景

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

function foo(path_to_m_file)disp(['正在运行'path_to_m_file])运行(path_to_m_file);结尾

函数 foo 只是获取一个 .m 文件的路径并尝试运行它.

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

./run_foo.sh $path_to_run_time $path_to_m_file

其中 path_to_m_file 是一个简单的 .m 文件,其中包含如下语句:

a = 2;

我收到以下错误:

错误使用 ==>跑MATLAB:运行:FileNotFound

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

中的以下两行

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

foo 读取 .m 文件的对应行.所以 .m 文件就在那里,MATLAB 引擎可以看到"它.事实上,我什至可以对使用 textscan 读取的字符串运行 eval.

所以我的问题是:

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

    更新:请参阅下面@strictlyrude27 的回答,以了解似乎是该问题的答案.

  2. 如果上述方法不起作用.有没有办法让 MATLAB 编译的函数运行 .m 文件,该文件可能在编译原始函数后 发生了变化?

我的第二个问题的动机:

我希望能够更新".m 文件是项目的一部分,无需重新编译整个项目.对此的任何想法将不胜感激.

解决方案

来自 MATLAB 编译器的 文档:

<块引用>

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

MATLAB Compiler 旨在让您可以部署锁定的功能.可部署的 MATLAB 文件在 MATLAB Compiler 加密它们时被挂起或冻结——从那时起它们不会改变.这并不意味着您不能部署灵活的应用程序——这意味着您必须在设计应用程序时考虑到灵活性.例如,如果您希望最终用户能够在两种不同的方法之间进行选择,则必须同时编译它们.

MCR 仅适用于构建组件时加密的 MATLAB 代码.任何动态生成新 MATLAB 代码的函数或进程都不会对 MCR 起作用.

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

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

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

<块引用>

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

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

<块引用><块引用>

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

Background

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

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

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

./run_foo.sh $path_to_run_time $path_to_m_file

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

a = 2;

I get the following error:

Error using ==> run
MATLAB:run:FileNotFound

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', '
');

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.

So my questions are:

  1. Why do I get the error above? Why doesn't foo run the .m file?

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

  2. If the above doesn't work. Is there a way to get a MATLAB-compiled function to run an .m file that may have changed after compiling the original function?

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.

解决方案

From the MATLAB Compiler's documentaton:

Compiled Applications Do Not Process MATLAB Files at Runtime

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.

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.

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.

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.

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

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

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

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天全站免登陆