Pass input arugments in .exe file generated by Matlab coder

Hello everybody,
it's the first time I'm working with the Matlab coder.
My aim is to generate an .exe file, where it's possible to pass arguments in order to run the program.
Up to now, I could actually manage to generate an .exe file which runs perfectly.
My issue is to pass arguments in this .exe file, since it's still a static program running every run with the same parameter values.
Assuming my generated file has the name "Test.exe" and one input is the parameter "dim" (so in Matlab it's Test(dim)), I'm running the program with "system('Test.exe dim')".
In order to check the value of the input "dim", I'm using fprintf for the console.
Further on, to enable the generation of the .exe, I've used:
if(isempty(dim)==0)
dim = 1;
end
Unfortunately, it doesn't matter which value dim has, it's always empty "[ ]".
For better illustrations, my example code:
Test(dim)
fprintf("The parameter dim has the value: %1.0f", dim)
if(isempty(dim)==0)
dim = 1;
end
end

 Accepted Answer

<You must modify a main.c> function to read/use an argument list when creating a standalone executable; it doesn't happen by default (as you've discovered).
Follow the above link in detail, it progresses through the steps of showing how/where the default main.c file is created and then how to modify a copy to include the things your application needs....eventually it gets to illustrating the use of the C command line argument functions to (in the example) read a specific input file instead of using the builtin default image the example starts with.
Follow the bread crumbs trail through the woods and, like Hansel and Gretel, you'll eventually get back from the witch's house to the land of Oz...or is it Kansas?

9 Comments

Awesome, thanks a lot!
Do you know how to clear variables in the coder?
In Matlab, I'm aware that you can use easily "clean", but the coder just ignores it while generating the c/c++ code.
C is static allocation, not dynamic that MATLAB handles; see <@Walter Roberson's Answer from a prior asking of the same question> for some more background...
Alright, my last question is about the output of my standalone exe. file.
Assume Output = Test(dim), how can I access "Output"?
My goal is to run Test.exe and to see "Output" either in a file or even better in the workspace of Matlab.
Right now, I can only see the console output via fprintf.
According here, "The only way to return values from compiled code is to either display it on the screen or store it in a file".
But how do you store the output, since save is not supported?
Is it possible to adjust the main.c file, so that I can obtain "Output"?
Moreover, I found this here, but is it really that difficult to extract data from a standalone application?
As a separate process, test.exe "knows nuthink!" about the MATLAB workspace and the OS security measures preventing malicious code also are an issue in managing to interact with another program.
To write to a file would use low-level C i/o of fwrite(), fread() for unformatted stream files or fprintf(), fscanf() for formatted files. There are further restrictions documented in the "Extended Capabilities" section of the documentation for each.
About the only way I can think of to actually return data to the workspace would be to use the system() call from MATLAB to dispatch the executable and capture its output (which would be dumped to standard output). If going to do something like that, then it would make more sense to turn the code into a mex file, not a standalone executable.
It's not clear just why you are trying to do this; don't know that sharing the use scenario would lead to any other solutions, but having a specfic context or objective might lead to some other bright ideas by somebody.
I am trying to do a parameter study in order to optimize the runtime of an algorithm.
Within Matlab, I could manage to generate a lot of data by varying the individual parameters so far.
Because I was always thinking that Matlab is optimized concerning matrix-vector multiplications (main computational effort of my algorithm), I wanted to compare the runtime with the one of a low-level programming language.
I wasn't aware that there is such a huge gap between Matlab and e.g. C/C++.
Maybe Matlab is optimized in this point regarding high-level programming languages, but still I am amazed, since the speedup is enormous.
So, my plan is to do the same parameter study as with Matlab, just with C/C++.
Due to my work at the University, my field of interest is dedicated to the time propagation of an electro-magnetic wave (simply expressed).
Within Matlab, I am able to visualize the propagation beautifully after each time step.
Against this background, I want to compute my entire program in C/C++ and visualize it with Matlab afterwards.
Besides, I'm collecting the data in an excel table generated by Matlab for a better overview.
Of course I was trying to generate a .mex file first only of the main computation of my program in the style of:
Initialization (Matlab) -> Computation (.mex) -> Visualization (Matlab)
However, I could observe that my .mex file runs almost as fast as the ordinary .m file (probably I need to optimize this, even though it's not always possible to get a speedup unfortunately, as stated out here).
Therefore, I try to take one step further and to have a workflow like:
Initialization + Computation (.exe) -> Visualization (Matlab)
As already said above, the runtime results are excellent, but I thought it would be easier to shift the data from an standalone application to Matlab.
To enable the visualization, I am storing the parameter of my program in a struct.
"I wasn't aware that there is such a huge gap between Matlab and e.g. C/C++."
In the computational part alone, MATLAB is built around optimized LAPACK routines for matrix calculations and thus is generally quite good in comparison.
Of course, there is much to making the best use of MATLAB syntax and to ensure one is getting the best performance from MATLAB that one can -- sometimes one can do wonders by some relatively simple refactoring or rewriting of an m file. <Yair> has a whole consulting business around such. Reading through his blogs would undoubtedly be highly educational if you're not already familiar with same.
The overhead in MATLAB generally is in the data handling in order to get to/from the builtin core routines; unfortunately for performance, the introduction of a lot of new data classes and the move away from purely procedural code to an OOP paradigm has really cause the size and performance of MATLAB overall to suffer. TMW continues to make progress in that regards, but recent releases simply are not as fast as earlier ones were...but, the place of MATLAB isn't necessarily to be the fastest kid on the block but to have the facilities for ease of development and visualization by the incorporation of all of these useful features. "There is no free lunch!"
Then I have to accept the circumstances for now and possibly try to get something out of it with .mex.
Still, thank you very much for the detailed answers and help.
Hey there,
I just wanted to say that I could manage it to store my relevant data by using fopen and fwrite while creating a .bin file.
Then, I've written a .m file which opens and plot my data from the .bin file.
It perfectly works, thanks a lot for your help!
Kewl...glad it all worked out for you.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 11 Sep 2023

Commented:

dpb
on 21 Sep 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!