exe file (simulink model build method rsim) wont update input variables? - matlab

i made exe file from simulink model using rsim method. i followed this link
how to import mat file to simulink?
that means i made simulinks input to mat file called "suma", and then comppiled it, "suma" is a mat file I save my variables from GUI.
the problem is that when i change my variables in GUI, and the "suma" varibales in that mat file change, my exe file still uses the old values that i used to compile it.
here is the code
a=get(handles.abroj, 'String');
b=get(handles.bbroj, 'String');
a=str2num(a);
b=str2num(b);
save('file_path_for_suma.mat','a','b');
system('file_path_for_exe_file') %this is for starting exe file?
zbroj3=load(file_path_to_output_exe_mat_file)

You're mixing up the way that Simulink works when simulating a model, and the way a simulation works when it has been converted to an executable.
A good example of the steps you will need to take can be found here: Using RSim Target for Parameter Survey.

Related

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.

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.

Workspace from a .m file won't import into a simulink model. MATLAB

I'm making a GUI with Matlab that runs an initialization file (full of variables), and then simulates a Simulink file with these variables. However, the Simulink model doesn't import these variables from the workspace into it so I get errors of undefined values, etc. Is there anyway to fix this? I'm not able to specifically tell the Simulink file what variables need to be used since I'm making this GUI for an overall usage of different initialization files/Simulink models, and the amount of variables from the initialization file is in the hundreds.
Here is my code (brief):
open_system(fullfile_loadSimulink)
run(fullfile_loadInitialization)
run(fullfile_loadInitialization2)
sim(fullfile_loadSimulink)
It's straight forward, the fullfileXXX is just the string and path to each file. This even works straight from the command line, so there must be something going on with the workspaces not matching up within the GUI.
Thanks in advance.
Sounds like your code is a function, not a script. Is that correct? If so, your function should be a script, otherwise the function has its own workspace and the Simulink model runs in the base workspace.

s-function in simulink MATLAB

I'm getting message
Error in S-function 'project1_simu/S-Function': S-Function 'chiu_sfcn' does not exist
whenever i run my simulink file which contains s-function block diagram. Any tips to resolving this problem?
You need to compile the s function using mex.
Then the mex32 output file needs to be on the matlab path.
For an S-Function, you need to make sure your C, Matlab, or Fortran Code is where Simulink expects it to be. My guess is your code exists, but isn't in the directory simulink expects.
Right click on the S-Function Block in your Simulink Project -> Choose S-Function Parameters.
Click on the Edit button next to the S-function name.
It will probably tell you the S-Function source cannot be found. It will then prompt you to either browse for the code or open an editor.
If you can't figure out where this file is located and you have the code your best bet is to open the Editor from this dialog box.
From there just paste the code and save it as 'chiu_sfcn'. You don't need to put a file extension, or browse to a different folder. It should automatically save it in the right folder for you.
It is possible that your version of Matlab is newer
than the version of Matlab used to produce the S-function.
If this is the case, I believe that the S-function
must be recreated in a newer version of Matlab
in order to interface with newer versions of Matlab.

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.