MATLAB deploytool simulink Undefined function 'load_system' - matlab

I am using a MATLAB m file code which loads a simulink file, runs it and evaluats it so many times inside a complicated parallel loop.
It runs perfect at MATLAB environment. When compiling the code via deploytool command, installing it and running it from command line, I would have a difficult time.
At the line where it is loading the model,
mymodel= ... ;
load_system(mymodel);
I get the following error
C:\Program Files\mymain\application>Warning: Name is nonexistent or not a directory: model
> In path at 109
In addpath at 86
In run_main at 3
Warning: Name is nonexistent or not a directory: data
> In path at 109
In addpath at 86
In run_main at 4
Warning: Name is nonexistent or not a directory: graphics
> In path at 109
In addpath at 86
In run_main at 5
Starting parallel pool (parpool) using the 'local' profile ... connected to 2 workers.
Initializing ...
Undefined function 'load_system' for input arguments of type 'char'.
Error in run_main (line 40)
MATLAB:UndefinedFunction
I don't care about warnings (as I don't care about most of other MATLAB warnings) however I get error about not recognizing function to open a simulink model ('load_system') :
Undefined function 'load_system' for input arguments of type 'char'.
I did a search and I found the following questions:
load_system using MCR
Deployment of Simulink Models
The first link says
I just got confused: is deployment tools about compiling to C++ or to .NET ?
Is is supposed to increase speed or becomming stand-alone is the only advantage?
The problem with both links is that they talk about strategy. But how to implement them? How can I compile a MATLAB code which uses load_system and sim command to load and simulate a model?
Can anybody bring step by step solution for a very simple example of a MATLAB code (m file) simulating a model and then compiling both of them?

The first sentence of your documentation screen shot is the important one. As mentioned by #Navan (in a comment), Simulink functionality is not compatible with MATLAB Compiler. That is, any m-code that uses Simulink functinality (load_system, sim, etc) will not execute when using the MATLAB Compiler.
Assuming that you have access to Simulink Coder, and that your model doesn't contain functionality that prevents it from being converted to c-code, then your only option is to develop your application in two parts:
With your Simulink model: Use Simulink Coder to create an executable from your model. There are several examples of doing this in the doc, including Using RSIM Target for Batch Simulations. There are various command line options for passing new parameter sets and inputs to the executable via a mat file. The results of the simulation (i.e what would normally be written to the MATLAB Workspace) gets written to a mat file created by the executable.
Within you MATLAB code: replace all Simulink functionality with a call (typically using the system function, as shown in the linked example) to the executable created in step 1. Prior to this call you'll need to create a mat file with all your parameters in it; and after the call you'll want to read results from the mat file created by the executable.
It's not a trivial process, but is do-able.

Related

Why is a variable undefined when trying to analyse .bat file in MATLAB?

I am really new to MATLAB. I have to use it for a project on 3D facial image analysis. I followed procedures described at Facial Landmarking Software.
I am using Windows. I get the following error message when I run compute_many.bat as required in the link:
Undefined variable "compute_many" or class "compute_many.bat".
I can see the compute_many.bat file generated in my folder from the code Get_compute_many_Windows, but I do not know why the variable is undefined.
I do not particularly understand:
Open a terminal window, cd to either the Training or Subjects directory and execute the following command to generate the .PLY files.
How to open a terminal window? Where to type compute_many.bat? Does this cause the error?

Simulink executable is not reading .mat files

I have a Simulink model wihich reads some data from a .mat file using a "From File" block. For ilustration's sake I created the following simple system which has the same problem I'm experiencing with my original model:
I need to generate a C code to run the model on another PC that does not have MATLAB. I generated the code and compiled it to validate it and I noticed that the executable is not reading the .mat file. No matter what signal I put on the file, the executable runs the model using the signal available at the moment of the compilation. I even erased the .mat file and the executable ran normally. Why is this happening? it is supposed that the "From File" block is there to read a .mat file, but it is not fulfilling its purpose.
NOTE: I am using MATLAB R2012b and Simulink version 8.0. I am using the Embedded Coder target (ert.tlc) for the code generation. I tried using the Rapid Simulation target (rsim.tlc) and the executable worked properly, however the execution time was higher. So I want to find a way to use the Embedded Coder target to have a lower execution time.

Why do I need to run a file several times in MATLAB before it stops throwing an error?

I have been trying to debug this weird bug where I run a script in matlab and it says that it cannot find some function that I have clearly defined in a folder and also obviously imported to the running script. However, after running the script a few times it suddenly accepts it knows where the location of my function is and runs. I find this really strange because that shouldn't be happening because imports are deterministic functions, so I have no idea why running it multiple times should make a difference.
To reproduce my bug you can find my code in my project's github page. Go/cd to the folder:
research/HBF_mat_lib/HBF1_multivariant_regression/om_simulations/h_add_cv
and
run:
test_debug_script
in MATLAB.
It show throw an error:
Undefined function 'learn_HBF1_SGD' for input
arguments of type 'HBF1_parameters'.
Error in get_best_trained_hbf1_model (line 37)
[ mdl_params, errors_train, errors_test ] =
train_func( X_train, y_train, mdl_params,
iterations,visualize, X_test,y_test,
eta_c,eta_t, sgd_errors);
Error in test_debug_script (line 11)
get_best_trained_hbf1_model(slurm_job_id,
task_id)
Error in run (line 96)
evalin('caller', [script ';']);
but if you try running it enough times it runs as it should.
Things I've tried to clear this bug is clear my matlab environment, restore my path to normal, at this point I have no idea what I can do because I have also printed out what path is before executing stuff and the folder seems to be in my path variable. At this point I have no idea what I can do and why after running a few times it runs, it should ALWAYS fail but it does not...
I'd venture it's because you're creating a reference to learn_HBF1_SGD in simulation_config.m before you put the function on the Matlab path via load_paths.m, which you've made dependent on the function handle's content. Given the current structure, I'd switch to declaring train_func_name explicitly, and then use str2func to create the handle after the paths have been loaded.
Subsequent runs of the file work because the execution did not stop until after the calls to addpath have been made. And since addpath "adds the specified folders to the top of the search path for the current MATLAB® session", the function was on the Matlab path for subsequent runs.

Run matlab script function after deploytool [duplicate]

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:
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.
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:
Run the code once in MATLAB to obtain your generated function.
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.
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.

Running an .m file from a MATLAB-compiled function

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:
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.
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:
Run the code once in MATLAB to obtain your generated function.
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.
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.