I know I can launch a executable in MATLAB with the !example.exe command or system(example.exe) as laid out in this question. However, when I want to pass arguments to the C++ program, anything I type is taken as a string. How can I use MATLAB variables?
For example, let's say example.exe was the following program:
int main(int argc, char *argv[]){
std::cout << argv[1] << std::endl;
}
When I call it from MATLAB as !example.exe hi, I get the output hi.
But what if my MATLAB script were actually:
hi = 'HELLO!';
!example.exe hi
My output is still hi, but I want it to be HELLO!.
How do I do this?
Thanks in advance!
You need to create a string using your variables and then run it using eval. For example:
n = 3;
command = sprintf('!example.exe %i', n);
eval(command)
Don't know if this is what you are looking for, but you can put the execution command together in a string using sprintf and then pass that to system. Something like:
hi = 'Hello!';
command = sprintf('example.exe %s', hi);
system(command);
Just use string concatenation:
hi = 'HELLO!';
system(['example.exe ' hi]);
Related
Long story short:
I created a program to get the path to a model file, do something with that information and pass on a different set of information, then i tried to create a wrapper for another program that basically catches the command line arguments and funnels some of them to my program to do its thing.
Simplified program flow.
Software --> args[all] --> Wrapper --> args[some] --> Program --> Output
The problem i now have is, calling my program on its own with hard-coded information or command line arguments equal to what the Wrapper would pass on works both flawlessly. The same goes for the Wrapper it works independently.
It also works to have the Wrapper pass on arguments and call the Program if the model information is wrong, which results in a simple string warning. But if the path to the model is correct it gets stuck in an endless loop and i don't know why. Separate runtimes are 8-11s for the Program and maybe 2s for the Wrapper, but i let it run for about 10min and nothing happened after some dummy print code.
Wrapper:
int main(int argc, char* argv[]){
if(argc > 1){
string tmp0 = argv[1];
string tmp1 = "./program " + tmp0;
const char* model = tmp1.c_str();
int status = system(model);
}
else{
cout << "No Model Information.\n";
}
return 0;
}
Parameters would be: Path to a model file and some other stuff, but only the path to the model would be passed on to the program in this case.
Program:
#include "program.hpp"
int main(int argc, char* argv[]){
if(argc > 1){
string tmp = argv[1];
const char* model = tmp.c_str();
cout << program(model);
}
else{
cout << "At least one defined Argument for the Model needs to be given.\n";
}
return 0;
}
Program Function Pseudo Code:
#include <stdio.h>
string program(const char* model){
string tmp
read(model);
if(model has variables) do
tmp = "has variables";
return tmp
}
--- Edit ---
The program function code is in pseudo code because its too complex and specific to list here without a load of libraries and other stuff being listed. If thats necessary i can try to provide it, but as it is i dont have a small or simple example i can give.
--- End edit ---
I don't understand enough about command line arguments to write sophisticated code, so i know this is more or less shitty, but i only need to be able to execute the program from the wrapper somehow and need to be able to read and pass on specific command line arguments. This is just the best i came up with.
Executing the Program code alone, with correct model information: ~10s execution time
Executing the Program code alone, with wrong model information: ~2s execution time
Executing the Wrapper code alone: ~2s execution time
Executing the Wrapper code, calling the Program code with wrong model information: ~2s execution time
Executing the Wrapper code, calling the Program code with correct model information: endless wait
I just dont get why it doesnt progress even though each works separately.
You can use fork() and execvp() in the unistd.h API. Depending on the return of fork() you can have the wrapper wait or end after launching the command.
Save the source file as args.cpp and compile it with the following command:
$ g++ args.cpp -o args && ./args -l -h -a
#include <cstdio>
#include <unistd.h>
#define SPAWNCMD "ls"
int main(int argc, char* argv[])
{
if(argc > 1)
{
printf("Command to spawn: %s ", SPAWNCMD);
for (unsigned i = 1; i < argc; i++)
printf("%s ", argv[i]);
printf("\n");
if (fork())
if (execvp(SPAWNCMD, argv) == -1)
{
printf("Incorrect termination of SPAWNCMD\n");
return 1;
}
}
else
{
printf("No Model Information.\n");
}
printf("Wrapper program end\n");
return 0;
}
How would I pass a command line variable though a system() command in c++.
I have tried using:
string i;
i = system("./findName.sh");
i += argv[1];
cout << i;
But when i run this it gives me my condition for wrong number of arguments i have written in my shell script.
This is the output I received when running my program with "./findName brandonw". Which is my executable file ran with the argument i want my shell script to run with.
The arguments you put are:
brandonw
usage: findName.sh [only_one_argument]
Just concatenate it to the command string.
string command = "./findName.sh";
command = command + " " + argv[1];
system(command.c_str());
Just rearrange your code a bit:
string i("./findName.sh ");
i += argv[1];
system(i.c_str());
cout << i;
Also note, that system doesn't return a std::string, but an implementation defined int value.
If you need to deal with ./findName.sh's output, you rather need pipe().
I have never used C++ before and my professor said this is supposed to be "very simple", but I can't figure it out. This is what I have so far:
#include <iostream>
#include <cstdio>
#include <cstdlib>
int main(int argc, char *argv[])
{
char command[50];
if (argc==2)
system(command, "./findName.sh", argv[1]);
}
return 0;
My shell script works when I run it by itself but I am not sure how to use a C++ program to run it. For the shell script, the user is supposed to enter a user ID like this:
./findName.sh userID
and the program returns the person's name from a file of names and user IDs like this:
LastName, FirstName
For the C++ program, it needs to pass the information the user enters to the shell script and return the same results.
As I said, I have never used C++ before so I don't know if any of this is right. It is a mix of things I have found online. Thank you for all of your help!!
The easiest way I find to do this is to use std::string as it allows you to concatenate stings easily.
#include <string>
#include <cstdlib>
int main(int argc, char* argv[])
{
std::string command = "./findName.sh";
if(argc == 2)
std::system((command + " " + argv[1]).c_str());
}
But to understand why this works you are going to need to study some books.
First you need to compile the program before you can run it. If your source code file name is progname.cpp you should be able to use this command:
g++ -o progname progname.cpp
And then run the program like this:
./progname userID
1) system("abc.sh " + para1 + " " + para2);
But this method have a limitation, the max length you can pass into is
MAX_ARG_STRLEN is defined as 131072 bytes=32 pages of memory.
2) open a file and read all the value to the file
system("abc.sh < yourfile.txt");
This method is a bit indirect, but you can put unlimited size of value to sh
inside the sh, use a normal read from buf ( or keyboard )
I am trying to run a face detection code in openCV and I don't want to use the command prompt to run it but I don't know what to give as an input argument to load_object_detect.
here is the sample from the code:
CvHaarClassifierCascade* cascade = load_object_detector(argv[2]);
In order for the above command to execute the code should be run from the command prompt which I am trying to avoid for now...
All I know is that its input type is const char* cascade_path...
try argv[2] = whatever path you want to put;
just before calling the function
Something like this
#include<iostream>
using namespace std;
int main(int argc , char *argv[])
{
argv[2] = "SuvP";
std::cout<<"Hey "<<argv[2]<<endl;
return 0;
}
The output is Hey SuvP
The arguments from command line are stored in the array argv. Alternatively we are filling data in the array within the code and not from the command line in this case.
Basically I am making a Python program and part of it needs to run a C++ executable, I am calling the exe with:
subprocess.call(["C:\\Users\\User\\Documents\\Programming\\Python\\Utilities\\XMLremapper\\TranslatorSource\\FileFixer.exe", "hi"])
But how do I make the C++ program read the input? I tried:
FILE * input = popen("pythonw.exe", "r");
cout<< input.getline() << endl << endl;
But that just outputs 0x22ff1c and definitely not "hi". What code is needed to pipe the input into the C++ program?
They are passed as parameters to the main function.
main(int argc, char *argv[])
argc is the length of argv. So it would be
main(int argc, char *argv[])
{
cout<<argv[1];
return 0;
}
If you just want to pass in a few arguments, an easy option is to read arguments on the command line, as suggested in another answer.
For more substantial input/output, where you'd naturally want to use cout or cin, a better option is to use subprocess.Popen. You can then write to and read from the other process as if they were file handles in python. For example:
proc = subprocess.Popen(["FileFixer.exe"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = proc.communicate("hi\n")
This tells python to run the process, passing in 'hi' followed by carriage return as standard input, which can then be read by cin in the C++ program. Standard output (the result of cout in C++) is then passed into the stdout list, and standard error is passed into stderr.
If you need more interactive communication, you can also access proc.stdout and proc.stdin as if they were filehandles in python (e.g. proc.stdin.write("hi\n")