How do I pass arguments into and out of my standalone executable?

286 views (last 30 days)
I want to be able to pass input and output arguments to my standalone application when I bang out to the DOS shell from MATLAB.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 3 Nov 2021
Edited: MathWorks Support Team on 4 Nov 2021

More Answers (4)

Antti on 26 Jan 2018
Note that in R2017b (and possibly beyond) on Windows 10, no output is printed on the Windows command prompt unless you uncheck "Do not display the Windows command shell (console) for execution" under "Additional runtime settings" in the Application Compiler tool of MATLAB Compiler.

Iaroslav Linder
Iaroslav Linder on 5 Nov 2018
Edited: Iaroslav Linder on 5 Nov 2018
Finally found a solution how to input and output variables in generated executables. Here is the example (Name the script main.m)
function out=main()
num=0; %this converts to double
num=input("Enter number");
coder.ceval('scanf','%lf',coder.ref(num)); % not '%f'
fprintf(1,'The squared value is %f\n',sqrt(double(num)));
Iaroslav Linder
Iaroslav Linder on 5 Nov 2018
it is even possible to let executable file accept command line arguments. But it requires a little trick to set the type of the second argument as the structure (struct 1 x:50) of type (char*)
function out=main(argc, argv)
fprintf(1,"Number of args %d\n",argc)
for i=1:argc
X = str2double(tmp);
if (imag(X)~=0||real(X)~=0)
fprintf(1,'%lf \n',X)
fprintf(1,'Summ %lf \n',X_sum)

Sign in to comment.

Steve Lim
Steve Lim on 13 Dec 2016
The examples show how to specify the input parameters to launch an application (exe). Within the application, what's the MATLAB source code look like to get the input parameter strings?
Thank you.
  1 Comment
Walter Roberson
Walter Roberson on 13 Dec 2016
Depending on whether you are expecting scalars or not, str2double() or use some sscanf() or even textscan() on the string. (You can pass a string as the first parameter to textscan to have it read, instead of passing a file identifier there.)

Sign in to comment.

Ewen Chan
Ewen Chan on 17 May 2018
Sorry for resurrecting a super old thread, but I have a question about this:
So in the script that I have now, at the start of it, I have declared some variables and assigned them values.
c0 = 3;
c1 = 100;
c2 = 500;
pi = 0.1;
p = 0.05;
N = 650;
e1 = 0.001;
e2 = 0.001;
If I want to create a standalone application, so that I can vary the numbers for those variables, how would I go about doing that?
Do I use the Application Compiler?
And if so, how do I go about doing that? This is my first attempt at a) trying to compile a standalone application and b) trying to pass input arguments that will change the variables that is currently at the top of my script/code.
Any help would be greatly appreciated.
Thank you.
Kristen Lancaster
Kristen Lancaster on 11 Feb 2019
Did you get it working? Could anyone provide input on my attempts to use command line arguments with a standalone created with MATLAB Coder?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!