MATLAB Answers

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

549 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.

More Answers (4)

Antti
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.

  0 Comments

Sign in to comment.


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
if coder.target('MATLAB')
num=input("Enter number");
else
coder.ceval('scanf','%lf',coder.ref(num)); % not '%f'
end
fprintf(1,'The squared value is %f\n',sqrt(double(num)));
out=int32(0);
end

  2 Comments

Iaroslav Linder
Iaroslav Linder on 5 Nov 2018
with strings, it can be tricky, so another example
function out=main()
num=char(zeros(100,1));
coder.ceval('scanf','%s',coder.ref(num));
fprintf(1,"The string is %s",num)
out=int32(0);
end
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)
X_sum=complex(0);
for i=1:argc
tmp=char(zeros(1,100));
coder.ceval("strcpy",coder.ref(tmp),argv(i));
X = str2double(tmp);
if (imag(X)~=0||real(X)~=0)
fprintf(1,'%lf \n',X)
X_sum=X_sum+X;
end
end
fprintf(1,'Summ %lf \n',X_sum)
out=int32(0);
end

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.
e.g.:
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.

  3 Comments

Walter Roberson
Walter Roberson on 18 May 2018
You would use MATLAB Compiler.
If you wanted the user to be able to set selective values then you might have an interface that looked something like
function MyMain(varargin)
v.c0 = 3;
v.c1 = 100;
v.c2 = 500;
v.pi = 0.1;
v.p = 0.05;
v.N = 650;
v.e1 = 0.001;
v.e2 = 0.001;
known_vars = fieldnames(v);
if mod(nargin,2) ~= 0
error('Expecting an even number of arguments');
end
for idx = 1 : 2 : nargin-1
if ~ismember(varargin{idx}, known_vars)
error('Argument "%s" is not a variable name I know', varargin{idx});
end
val = str2double(varargin{idx+1});
if isnan(val)
error('Value "%s" for variable "%s" is not a numeric scalar', varargin{idx+1}, varargin{idx});
end
v.(varargin{idx}) = val;
end
That is, the user would call the executable with alternating variable names and numeric scalars on the command line, such as
MyMain.exe c2 750 e1 0.025
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!