### 3.7 Interface to MATLAB®

MATLAB is a proprietary software platform designed for solving engineering and scientiﬁc problems. Although it provides some toolboxes that facilitate statistical analysis, in general, and econometric modeling, in particular (mostly time-series models), MATLAB is primarily known for its extensive and easy to use language. This is largely a matrix-based language, speciﬁcally designed for solving computational problems.

BayESmatlab() function provides a convenient interface to MATLAB, which allows the user to:

• pass BayES matrices and datasets as input to MATLAB
• request MATLAB to execute code written in its native language
• retrieve output from MATLAB and store it in BayES matrix items; all data to be returned from MATLAB are stored inside a BayES structure item

The general syntax of the matlab() function is the following:9

$\left[$<structure name> = $\right]$ matlab( <MATLAB .m file>
$\left[$, "data"=<list of matrices/datasets to pass to MATLAB> $\right]$
$\left[$, "return"=<list of matrices to retrieve from MATLAB> $\right]$
$\left[$, "options"=<string of options when starting MATLAB> $\right]$
);

where:

• <structure name> is a BayES id value which will be associated with the BayES structure that the matlab() function returns. This structure will contain any MATLAB matrices that the user requests to be returned to BayES (using the "return" option) after execution of the MATLAB .m ﬁle completes.
• <MATLAB .m file> is a string pointing to the ﬁle which contains the code (written in MATLAB’s language) that MATLAB will be requested to execute. If this ﬁle is not in the current directory then the ﬁle name must be prepended by the path to the ﬁle, either in absolute terms (eg. "C:/MyFiles/myFile.m") or relative to the current working directory (eg. "../myFile.m"). This is the only mandatory argument of the matlab() function.
• "data" speciﬁes the data that will be passed as input to MATLAB. These can be either BayES datasets or matrices. <list of matrices/datasets to pass to MATLAB> is a list of the id values of matrices or datasets (comma-separated names inside curly brackets), as they appear in the MATLAB .m ﬁle. These matrices or datasets must be deﬁned in the current workspace. When either a BayES matrix or dataset is passed as input to MATLAB then it becomes available in MATLAB as a MATLAB matrix. That is, BayES datasets are stripped of their additional information (most importantly, variable names and dataset structure) and only the raw data are passed to MATLAB.
• "return" speciﬁes the MATLAB matrices that will be returned to BayES when execution of the MATLAB .m ﬁle completes. <list of matrices to retrieve from MATLAB> is a list of id values (comma-separated id values inside curly brackets) that specify the names of the matrices that should be returned from MATLAB, as they appear in the MATLAB .m ﬁle. Any MATLAB matrix that is returned will be stored in BayES as a matrix and all returned matrices are grouped together into a BayES structure. Passing other data types (structures, cells, strings, etc.) between BayES and MATLAB is not supported.
• "options" speciﬁes the MATLAB options to be passed to the command-line argument when calling MATLAB. All these options should be provided as a single string. The default value is:
"$-$nojvm $-$nodesktop $-$nodisplay $-$nosplash $-$minimize $-$wait $-$r"
and has the following eﬀects:
• nojvm: starts MATLAB without the JVM software. Features that require Java software (such as the desktop tools and graphics) are not supported.
• nodesktop: runs the JVM software without opening the MATLAB desktop
• nodisplay: starts the JVM software without starting the MATLAB desktop
• nosplash: does not display the splash screen during startup
• minimize: minimizes the MATLAB window
• wait: blocks the script from continuing until the results from MATLAB are generated.
• r: executes the MATLAB statement, speciﬁed as a string or as the name of a MATLAB script or function. This should always be used as the last option because BayES appends to this option the location of the provided MATLAB .m ﬁle.

As the matlab() function executes with the above options, MATLAB attempts to print output on the system’s command console. BayES grabs this output and redirects it to the BayES main console in real time. This output is entirely determined by MATLAB and the commands contained in the MATLAB .m ﬁle provided to matlab().

The sample script ﬁle in "\$BayESHOME/Samples/5$-$Interfaces/matlab" contains an example of using the matlab() function, along with a simple MATLAB .m ﬁle. The MATLAB interface is also accessible from the BayES main menu via Interfaces MATLAB®.

9Arguments inside square brackets are optional. Optional arguments passed to the matlab() function can be provided in any order, but always after the mandatory argument (MATLAB .m ﬁle). Optional arguments always come in pairs (eg. "data"={myDataset,myMatrix}).

Share this content:

© 2016–20 Grigorios Emvalomatis