#include <MatlabInterface.h>
Collaboration diagram for PLearn::MatlabInterface:
Public Member Functions | |
MatlabInterface (string matlab_file_header, string matlab_file="", string id="", bool launch_in_background=false, bool erase_tmp_files=true) | |
MatlabInterface (vector< string > matlab_file_header, string matlab_file="", string id="", bool launch_in_background=false, bool erase_tmp_files=true) | |
Popen * | launch () |
bool | launchAndWaitFor (string matlab_end_answer) |
Static Public Member Functions | |
string | path () |
returns where to find appropriate .m files will be appended to the matlab path | |
void | eigs_r11 (RowMapSparseMatrix< real > &A, Mat &evectors, int d, string which_eigenvalues, bool erase_tmp_files=true) |
void | eigs_r11 (RowMapSparseMatrix< real > &A, Vec &evalues, int d, string which_eigenvalues, bool erase_tmp_files=true) |
void | eigs_r11 (RowMapSparseMatrix< real > &A, Mat &evectors, Vec &evalues, int d, string which_eigenvalues, bool erase_tmp_files=true) |
Public Attributes | |
Popen * | matlab |
The communication object that contains the Matlab command line. | |
string | matlab_file |
The pre-defined M-file. | |
string | matlab_file_header |
The user-supplied header, that will be concatenated to the top of the M-file. | |
Protected Attributes | |
string | id |
This is a unique id for a particular Matlab execution (to avoid conflicts). | |
bool | launch_in_background |
Some flags. | |
bool | erase_tmp_files |
Suppose you want to execute the following matlb lines:
load mymat -ascii; [U,S,V] = svd(mymat); save U.dat U -ascii; save S.dat S -ascii;
then you just do the following:
MatlabInterface matlab("load mymat -ascii; [U,S,V] = svd(mymat); save U.dat U -ascii; save S.dat S -ascii; fprintf(1,'done
');"); matlab.launchAndWaitFor("done");
Where you should note the fprintf of 'done' in the matlab instructions so as to be able to detect the end of the matlab execution.
Another way to use this class is to concatenate these explicitly given lines of code with an existing matlab file. Suppose you have defined the file foo.m :
file foo.m
a = 1; a = b + c;
%%%%%%%%%%
This execution of this file, by itself, would generate an error from the Matlab interpreter, stating that the variable b is not defined. You can then declare a MatlabInterface instance, with the line "b = 2;" as the header, the program will acquire a correct meaning. The underlying M-file will then become (exactly) :
b = 2; file foo.m
a = 1; a = b + c;
%%%%%%%%%%
Another usage that you may find interesting is passing a matrix from a PLearn program to a Matlab program. Suppose you have defined a RowMapSparseMatrix M in a PLearn class. You want to pass it to a Matlab program, let's say foo.m again. First you have to export M to a format that Matlab can read :
M.exportToMatlabReadableFormat("Mijv.dat");
You can then generate a header using a STL vector :
vector<string> header; header.push_back("load Mijv.dat;"); header.push_back("M = spconvert(Mijv);");
You can then use that header, along with a M-file that makes use of the sparse matrix M, to initialize a MatlabInterface.
NOTE : to avoid any process conflicts at run-time, the M-file being executed is not the one supplied, but a new one, that has a unique id. This is the case, even if you want to execute a M-file that requires no header.
An example of application of MatlabInterface is given after this class definition, for finding eigen-pairs of a symmetric matrix using the eigs() program of matlab r11 on a RowMapSarseMatrix.
Definition at line 135 of file MatlabInterface.h.
|
Use this constructor if your entire header is contained in a string. Be careful to delimitate the lines with ' Definition at line 63 of file MatlabInterface.cc. References erase_tmp_files, launch_in_background, matlab_file, and matlab_file_header. |
|
Definition at line 73 of file MatlabInterface.cc. References erase_tmp_files, launch_in_background, matlab_file, and matlab_file_header. |
|
|
|
|
This method will run the Matlab interpreter on the prepared M-file. A pointer to the Popen communication object is returned, for you to manage the termination of the program. One way to do that is to poll the "in" field of the Popen object, waiting for an answer from Matlab : MatlabInterface matlab(...); Popen* p = matlab.launch(); string answer; do { p->in >> answer; } while (answer != <desired>); Definition at line 84 of file MatlabInterface.cc. References c_str(), PLearn::endl(), launch_in_background, matlab, matlab_file, matlab_file_header, and PLearn::newFilename(). |
|
The method of "waiting for Matlab" described above is encapsulated in this function, where all you have to supply is the Matlab answer stating that the program is done. This method will also stop in case of matlab PLERROR(detected with the string "???"), and will return false in that case (return true if everything works). Definition at line 107 of file MatlabInterface.cc. References c_str(), PLearn::endl(), PLearn::PStream::eof(), erase_tmp_files, PLearn::Popen::in, launch_in_background, matlab, matlab_file, matlab_file_header, and PLearn::newFilename(). Referenced by eigs_r11(), and PLearn::matlabR11eigs(). |
|
returns where to find appropriate .m files will be appended to the matlab path
Definition at line 142 of file MatlabInterface.h. References PLERROR. |
|
Definition at line 250 of file MatlabInterface.h. Referenced by launchAndWaitFor(), and MatlabInterface(). |
|
This is a unique id for a particular Matlab execution (to avoid conflicts).
Definition at line 245 of file MatlabInterface.h. |
|
Some flags.
Definition at line 248 of file MatlabInterface.h. Referenced by launch(), launchAndWaitFor(), and MatlabInterface(). |
|
The communication object that contains the Matlab command line.
Definition at line 213 of file MatlabInterface.h. Referenced by eigs_r11(), launch(), launchAndWaitFor(), and PLearn::matlabR11eigs(). |
|
The pre-defined M-file.
Definition at line 216 of file MatlabInterface.h. Referenced by launch(), launchAndWaitFor(), and MatlabInterface(). |
|
The user-supplied header, that will be concatenated to the top of the M-file.
Definition at line 220 of file MatlabInterface.h. Referenced by eigs_r11(), launch(), launchAndWaitFor(), MatlabInterface(), and PLearn::matlabR11eigs(). |