How to pass arguments to exe and return output from exe in MATLAB
9 views (last 30 days)
Show older comments
I have an executable file (.exe) for simple program addition of two inputs. How to pass arguments to .exe and get output.
for example
Out = system( ' "Test.exe" , ar0,arg1')
Here, i want to add two numbers arg0 = 3, arg1=4 and out will be value 7.
How to pass argument to .exe and retun output ? the abaove method is not working.
Note: 'Test.exe' is created by simple c-proram.
2 Comments
Answers (1)
Walter Roberson
on 14 Dec 2022
Your C program expects 16 bit binary inputs to main.
When a C main program is called, it is called with either two parameters or (common enough to be described in the standards but not mandatory) three parameters.
The first parameter is an int (typically 32 bits but might be 16 bits) that is most commonly referred to as argc . It holds the number of command line arguments that have been passed in to the program.
The second parameter is a char *[] that is most commonly referred to as argv . It is an array of pointers to character vectors, with the intention that each one is a character vector command line argument.
So in order to be able to pass in 3 into the first parameter, you need to pass 3 command line arguments. Well, except for the fact that your inputs are 16 bits and you would have to experiment to see whether the 3 got passed in the high byte pair or the low byte pair, so you might need to pass in 3*65536 = 196608 command line arguments in order to get the 3 into the right place to be pulled out in the x parameter.
In order to pass in 4 into the second parameter, your argv pointer would have to have 0x0004 one of 16 bit subsets of the 64 bit pointer. Or maybe 0x0400 depending on how the byte order goes for a 16 bit integer... you would need to experiment. But you have the problem that the command line processor that creates argc and argv is not going to use any particular pointer alignment.
Now, supposing that you managed to pass in the right 196608 command line arguments to get the values 3 and 4 into the right places to become available as inputs to your main(). You do the addition, and you store the result in the file-scope variable z which is not declared as extern . So the formal interface to C does not give access to that variable to the outside world, short of using debugger tools.
You want the return value of system() to be 7 in this case. The first output parameter of system() is the status code returned by the program. The status code is an int, and the way that a C program designates a value as the status code is to use return with the value, such as return 7 . When a C program does not use return then the default value put into the status code is 0. Your code does not return anything, so the default of 0 is going to be used.
So... your program calculates based upon the argument count and internal details of the pointer-to-pointers; stores the result as a file scope variable with no way to access the result short of a debugger; and returns a status code of 0.
You just might somehow manage to hack the parser to get binary 3 and 4 as inputs to your code, but you are not going to get the 7 out short of using a debugger.
So.... to use your program and get the result back as the first output parameter of system(), you would need to write a debugger script that invoked a debugger, ran the program with the appropriate arguments, fished inside the executable to find z and pull out its value, and then return that value of z from the debugger.
Have you ever considered the possibility that it might be easier to alter your C code to use the standard interfaces for input and output values ?
0 Comments
See Also
Categories
Find more on Biological and Health Sciences in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
