I have a C++ code opening a file and rewriting the file and I want to execute the function thro MATLAB
Show older comments
As I mentioned, I am now having a C++ code opening a file and rewriting the content, and I want to use mex.h to call the function in MATLAB.
for example, I have:
void Write_File(){
ofstream file("abc.txt");
double X=0;
file << setw(12) << 0.0 << " " << setw(12)<< X << endl;
return 0;
}
and now I want to do that in MATLAB. How should I define the mexFunction? what are the arguments, say nlhs, nxArray?
1 Comment
James Tursa
on 14 Feb 2013
Why is your void function returning a value?
Answers (1)
James Tursa
on 14 Feb 2013
Edited: James Tursa
on 14 Feb 2013
If you have no inputs or outputs, you can simply create this file:
// writefile.cpp
#include "mex.h"
// add any other supporting code here
void Write_File()
{
ofstream file("abc.txt");
double X=0;
file << setw(12) << 0.0 << " " << setw(12)<< X << endl;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
Write_File();
}
Mex it at the MATLAB command line with:
mex writefile.cpp
If you get an interactive prompt asking about compilers, enter Y (have MATLAB look for them) and then select a C++ compiler from the list. Note that the LCC compiler that ships with MATLAB is strictly a C compiler, not a C++ compiler.
Call it just like any other MATLAB function:
writefile
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) 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!