Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

PLearn::MatlabInterface Class Reference

#include <MatlabInterface.h>

Collaboration diagram for PLearn::MatlabInterface:

Collaboration graph
[legend]
List of all members.

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)
Popenlaunch ()
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

Popenmatlab
 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

Detailed Description

This class permits the execution of a Matlab sub-routine from within a PLearn program. The parametrization of the sub-routine (if required) is performed by the concatenation of a set of "lines" (a header), optionnally followed by an existing M-file (Matlab program file format). Here is an example that shows how it works :

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.


Constructor & Destructor Documentation

PLearn::MatlabInterface::MatlabInterface string  matlab_file_header,
string  matlab_file = "",
string  id = "",
bool  launch_in_background = false,
bool  erase_tmp_files = true
 

Use this constructor if your entire header is contained in a string. Be careful to delimitate the lines with '
'. If matlab_file=="" then no matlab_file is concatenated.

Definition at line 63 of file MatlabInterface.cc.

References erase_tmp_files, launch_in_background, matlab_file, and matlab_file_header.

PLearn::MatlabInterface::MatlabInterface vector< string matlab_file_header,
string  matlab_file = "",
string  id = "",
bool  launch_in_background = false,
bool  erase_tmp_files = true
 

Definition at line 73 of file MatlabInterface.cc.

References erase_tmp_files, launch_in_background, matlab_file, and matlab_file_header.


Member Function Documentation

void PLearn::MatlabInterface::eigs_r11 RowMapSparseMatrix< real > &  A,
Mat evectors,
Vec evalues,
int  d,
string  which_eigenvalues,
bool  erase_tmp_files = true
[static]
 

Definition at line 218 of file MatlabInterface.cc.

References c_str(), PLearn::RowMapSparseMatrix< T >::exportToMatlabReadableFormat(), header, PLearn::Popen::in, launchAndWaitFor(), PLearn::RowMapSparseMatrix< T >::length(), PLearn::loadAsciiWithoutSize(), PLearn::looksNumeric(), PLearn::Mat, matlab, matlab_file_header, PLearn::newFilename(), PLERROR, PLWARNING, PLearn::remove_extension(), PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::toint(), PLearn::tostring(), and PLearn::Vec.

void PLearn::MatlabInterface::eigs_r11 RowMapSparseMatrix< real > &  A,
Vec evalues,
int  d,
string  which_eigenvalues,
bool  erase_tmp_files = true
[static]
 

Definition at line 334 of file MatlabInterface.cc.

References c_str(), PLearn::RowMapSparseMatrix< T >::exportToMatlabReadableFormat(), header, PLearn::Popen::in, launchAndWaitFor(), PLearn::loadAsciiWithoutSize(), PLearn::looksNumeric(), matlab, matlab_file_header, PLearn::newFilename(), PLERROR, PLWARNING, PLearn::remove_extension(), PLearn::TVec< T >::resize(), PLearn::toint(), and PLearn::tostring().

void PLearn::MatlabInterface::eigs_r11 RowMapSparseMatrix< real > &  A,
Mat evectors,
int  d,
string  which_eigenvalues,
bool  erase_tmp_files = true
[static]
 

Definition at line 279 of file MatlabInterface.cc.

References c_str(), PLearn::RowMapSparseMatrix< T >::exportToMatlabReadableFormat(), header, PLearn::Popen::in, launchAndWaitFor(), PLearn::RowMapSparseMatrix< T >::length(), PLearn::loadAsciiWithoutSize(), PLearn::looksNumeric(), matlab, matlab_file_header, PLearn::newFilename(), PLERROR, PLWARNING, PLearn::remove_extension(), PLearn::TMat< T >::resize(), PLearn::toint(), and PLearn::tostring().

Popen * PLearn::MatlabInterface::launch  ) 
 

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().

bool PLearn::MatlabInterface::launchAndWaitFor string  matlab_end_answer  ) 
 

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().

string PLearn::MatlabInterface::path  )  [inline, static]
 

returns where to find appropriate .m files will be appended to the matlab path

Definition at line 142 of file MatlabInterface.h.

References PLERROR.


Member Data Documentation

bool PLearn::MatlabInterface::erase_tmp_files [protected]
 

Definition at line 250 of file MatlabInterface.h.

Referenced by launchAndWaitFor(), and MatlabInterface().

string PLearn::MatlabInterface::id [protected]
 

This is a unique id for a particular Matlab execution (to avoid conflicts).

Definition at line 245 of file MatlabInterface.h.

bool PLearn::MatlabInterface::launch_in_background [protected]
 

Some flags.

Definition at line 248 of file MatlabInterface.h.

Referenced by launch(), launchAndWaitFor(), and MatlabInterface().

Popen* PLearn::MatlabInterface::matlab
 

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().

string PLearn::MatlabInterface::matlab_file
 

The pre-defined M-file.

Definition at line 216 of file MatlabInterface.h.

Referenced by launch(), launchAndWaitFor(), and MatlabInterface().

string PLearn::MatlabInterface::matlab_file_header
 

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().


The documentation for this class was generated from the following files:
Generated on Tue Aug 17 16:23:06 2004 for PLearn by doxygen 1.3.7