Drake
call_matlab.h File Reference

Utilities for calling Matlab from C++. More...

#include <memory>
#include <string>
#include <vector>
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "drake/common/eigen_types.h"
#include "drake/common/proto/matlab_rpc.pb.h"
Include dependency graph for call_matlab.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  MatlabRemoteVariable
 Holds a reference to a variable stored on the matlab client, which can be passed back into a future lcm_call_matlab call. More...
 

Namespaces

 drake
 
 drake::common
 
 drake::common::internal
 

Functions

void ToMatlabArrayMatrix (const Eigen::Ref< const Eigen::Matrix< bool, Eigen::Dynamic, Eigen::Dynamic >> &mat, MatlabArray *matlab_array, bool is_vector)
 
void ToMatlabArrayMatrix (const Eigen::Ref< const Eigen::MatrixXd > &mat, MatlabArray *matlab_array, bool is_vector)
 
void ToMatlabArrayMatrix (const Eigen::Ref< const Eigen::MatrixXi > &mat, MatlabArray *matlab_array, bool is_vector)
 
void ToMatlabArray (const MatlabRemoteVariable &var, MatlabArray *matlab_array)
 
void ToMatlabArray (double var, MatlabArray *matlab_array)
 
void ToMatlabArray (int var, MatlabArray *matlab_array)
 
void ToMatlabArray (const std::string &str, MatlabArray *matlab_array)
 
template<typename Derived >
void ToMatlabArray (const Eigen::MatrixBase< Derived > &mat, MatlabArray *matlab_array)
 
void AssembleCallMatlabMsg (MatlabRPC *)
 
template<typename T , typename... Types>
void AssembleCallMatlabMsg (MatlabRPC *msg, T first, Types...args)
 
std::unique_ptr< google::protobuf::io::FileOutputStream > CreateOutputStream (const std::string &filename)
 
void PublishCall (google::protobuf::io::FileOutputStream *praw_output, const MatlabRPC &message)
 
void PublishCallMatlab (const MatlabRPC &message)
 
template<typename... Types>
MatlabRemoteVariable CallMatlabSingleOutput (const std::string &function_name, Types...args)
 Special cases the call with one output. More...
 
template<typename... Types>
std::vector< MatlabRemoteVariable > CallMatlab (int num_outputs, const std::string &function_name, Types...args)
 Invokes a mexCallMATLAB call on the remote client. More...
 
template<typename... Types>
void CallMatlab (const std::string &function_name, Types...args)
 Special cases the call with zero outputs, since it's so common. More...
 
template<typename T >
MatlabRemoteVariable NewRemoteVariable (T value)
 Creates a new remote variable with the corresponding value set. More...
 

Detailed Description

Utilities for calling Matlab from C++.

Provides a simple interface for (one-directional) RPC to a simple matlab remote client. Methods are provided to serialize our favorite data types into protobuf and then published to a file. The interface is modeled after mexCallMATLAB https://www.mathworks.com/help/matlab/apiref/mexcallmatlab.html but we use C++11 to provide a much nicer interface.

To play the remote calls in matlab, simply run call_matlab_client from your matlab terminal. For synchronous playback, use a named pipe by running mkfifo /tmp/matlab_rpc in a bash terminal.

The primary use case that this was designed for was to make MATLAB plotting available in C++ without requiring the C++ code to link against MATLAB in any way... (if MATLAB is not present, the messages simply fall on deaf ears).

Support for multi-function commands is provided by allowing return values to be stored on the remote client, and reused by a simple "remote variable reference" that is kept by the publisher.

See call_matlab_test.cc for some simple examples.