1

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

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.

Community
  • 1
  • 1
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564

2 Answers2

4

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.

Dang Khoa
  • 5,693
  • 8
  • 51
  • 80
  • Thanks @strictlyrude27. Sorry, I should have said that I had seen that, but I was surprised that I was able to run `eval` on lines that I read with `textscan`, so I'm still not sure why `run` doesn't work, but `eval` does. I'll update this on the OP. With this, I'm still wondering if there is an answer to my second question. – Amelio Vazquez-Reina Sep 13 '11 at 23:50
  • `Run()` uses the normal mechanism of calling a function defined on the filesystem. If the compiled Matlab code could run normal `.m` files from disk, you could compile a little GUI that reproduced the full Matlab IDE functionality and bypassed Matlab's licensing mechanism. `Eval` is more limited since it only evaluates expressions and doesn't define functions (and is slow); on the one hand it's not enough to build the whole language from; on the other hand it's so widely used that they really couldn't disable it and have compiled programs still work. – Andrew Janke Dec 04 '14 at 09:26
0

You can read read an m file, line by line and execute each line with the eval() function. There are restrictions on the format of the m file (no line breaks for example, each line must contain a complete MATLAB statement) but it does work and can add to your run time environment inside the compiled application. I use this technique to allow users to define configuration and data files for a compiled application I have developed.

Clearly, if your end user provides a poorly formed m file to evaluate, you will end up with difficult to resolve bugs.

Nigel Davies
  • 1,640
  • 1
  • 13
  • 26