Reputation: 571
Background:
I'm using Matlab with C++ for plotting purposes. In my present process, I need to pass a variable to matlab using matlab's function and also a string which will name the variable in matlab environment. The function I'm calling is engPutVariable(ep, "varMatlab" , varC++)
where ep is the object of class (matlab)Engine, varC++
is the name of the variable inside C++ which I am passing and varMatlab
will be the name of the same variable when is stored in matlab workspace. The process I use needs 4 lines of code to pass a single variable which does the same job for each variable I pass, hence I want to write a function which has the arguments of name of the variable in c++ and the name of the variable in matlab which needs to be in " " quotes.
Question:
As the function requires the name of the variable to be passed inside " " (double quotes), I want to pass a string which will take the place of varMatlab inside the quotes. I tried using inserting the " " with the string itself but it seems that is not working in this case. Any help will be really helpful.
I wrote the following function to do the same but the string name
should be inside the " " which I dont know how to pass.
void putVar(double* var,int N, string name, Engine *ep){
double row = N, col = N;
mxArray *matlab = mxCreateDoubleMatrix(row, col, mxREAL);
double *pa = mxGetPr(matlab);
memcpy(pa, var, sizeof(double)*row*col);
engPutVariable(ep, "name" , matlab);
}
Upvotes: 0
Views: 6308
Reputation: 10808
Insert the " as \". As " is a special symbol for strings, it needs to be escaped. So the string "Hello World" with the quotes looks like this in C++
std::cout << "\"Hello World\"\n";
Regards Tobias
Upvotes: 2
Reputation: 15501
I believe you're confused about needing the quotes as part of the string. I think all you need is access to the c-string inside the C++ string. Get rid of the quotes and try name.c_str()
.
Upvotes: 1
Reputation: 10061
Do you mean escaping?
The word "escaping" means in this context: You cannot use the character "
as content of a string becauase it's part of the c++ language.
So you have to escape it in order to make the string literally contain "name"
.
For this example, your code would look like this:
void putVar(double* var,int N, string name, Engine *ep){
double row = N, col = N;
mxArray *matlab = mxCreateDoubleMatrix(row, col, mxREAL);
double *pa = mxGetPr(matlab);
memcpy(pa, var, sizeof(double)*row*col);
engPutVariable(ep, "\"name\"" , matlab);
}
Did I misunderstood you?
Upvotes: 1