Compatibility Guidelines for MATLAB Compiler
Version 1.4
© 2024 MonkeyProof Solutions B.V.
T: +31 (0)76 8200 314
Table of Contents
Compatibility Guidelines for MATLAB Compiler
Introduction
The goal of this set of coding standards that can be automatically checked is to help you write MATLAB code that is Compiler-compatible. The resulting code will be more suitable for deployment using the MATLAB Compiler. For more information on MATLAB Compiler, see the MathWorks website.
If you are looking for more generic coding standards that are not specifically aimed at Coder-compatibility, see our MonkeyProof Coding Standard for MATLAB.
Automatic compliance checking
The coding standards described here can be checked using CC4M by MonkeyProof Solutions.
They are configured in the predefined CoderCompatibility
configurations set available with the tool. For instructions on how to check your code against coding standards, see the video below. The steps are also described below the video.
You can check your code for compliance with the guidelines described here as follows:
- Get CC4M and install per installation instructions.
- Open CC4M in one of two ways:
- Click the shortcut created at the top of the screen next to the documentation search bar: .
- Run
monkeyproof.cc4m.start()
from the command window.
- Select whether you want to check one or more files, all files in a folder, or all files in a MATLAB Project.
- Select what files/folder/project to check.
- Further customization is available, for example checking a folder including or excluding all subfolders.
- Click the Run button at the bottom to start checking your code.
The guidelines that require human judgment or are simply not possible to check statically in MATLAB are not described here.
Guideline template
Every guideline is structured as follows:
Guideline name
ID | A unique guideline identifier. |
Title | Brief description of the guideline. |
Priority | Priority of the guideline can be one of Mandatory, Strongly recommended and Recommended. Exactly what each of these means for your case is up to you to decide. |
Description | A more elaborate description of the guideline. Can include the reasoning behind the guideline: how does your code improve when applying the guideline? |
Rationale | One or multiple words describing what the guideline is about. Examples: Compatibility, Stability. |
avoid
<< a section with examples on what to avoid >>
instead use
<< a section with examples on how to code properly >>
MATLAB path
ID | MCC-1 |
Title | Adapting the MATLAB and Java class path shall be avoided. |
Priority | Strongly recommended |
Severity level | 2 |
Description | Runtime changes to the MATLAB and Java class path shall be avoided. |
Rationale | In a deployed application, the MATLAB and Java paths are fixed and cannot change. See Mathworks documentation. |
avoid
Avoid calling the following functions, as they manipulate either the MATLAB path or the Java Class path:
addpath(_)
rmpath(_)
path(_)
javaaddpath(_)
javarmpath(_)
javaclasspath(_)
instead use
As a deployed application can not adapt the MATLAB or Java class path, the definition of these paths should be separated from the code that will be deployed. For example, have a separate script or function, or use MATLAB Projects, to manages the path during development.
Scripts
ID | MCC-5 |
Title | Do not use scripts. |
Priority | Mandatory |
Severity level | 3 |
Description | Do not use scripts because these are not supported by the MATLAB Compiler. |
Rationale | Compatibility, scripts are allowed in some use-cases, but not in others, see MathWorks documentation |
Pragma required
ID | MCC-7 |
Title | Functions called from feval and eval constructions shall be included manually. |
Priority | Recommended |
Severity level | 6 |
Description | Functions called from feval and eval constructions shall be included manually using a #function pragma to ensure that the called code is included in the archive. |
Rationale | Portability |
avoid
[file] = feval('myfun', ...));
instead use
%#function myfun
[file] = feval('myfun', ...));
An alternative to using the pragma is the -a
option for the mcc
command. See MathWorks documentation.
Note: currently CC4M only highlights the calls to feval
and eval
, manual verification if the #function
pragma is used, is still required.
Functional differences
ID | MCC-8 |
Title | The use of functions with (potential) different behaviour when deployed, shall be avoided. |
Priority | Recommended |
Severity level | 6 |
Description | The use of functions with (potential) different behaviour when deployed, shall be avoided if possible. Otherwise their use needs manual review for documented reasons. |
Rationale | Portability |
manual review required
The following table shows the reviewing aspect.
Function | Rationale for review | Doc |
---|---|---|
help | MATLAB file comments are stripped out before MATLAB Runtime encryption. Note: actual behaviour seen in R2022b is a runtime error! | link |
printdlg | In compiled mode, only one argument can be present in a call to printdlg | link |
which | In compiled mode, which does not search the current folder. | link |
open | In compiled mode, open does not search the current folder. | link |
pwd | When compiled, the start directory of an application is context-specific, so using pwd might result in unexpected values. | |
matlabroot | Alternative to matlabroot , ctfroot can be usefull, but any folder operation should be programmed with care. For more information on ctfroot see MathWorks documentation. Also note the following Answer. | |
isfile | isfile(fileName) returns 1 if fileName is a file located on the specified path or in the current folder. Otherwise, isfile returns 0.Therefore, if the current folder in deployed mode is different, the result may be different, in case relative paths are used. | |
isfolder | isfolder(folderName) returns 1 if folderName is a folder located on the specified path or in the current folder. Otherwise, isfolder returns 0.Therefore, if the current folder in deployed mode is different, the result may be different, in case relative paths are used. |
Unsupported functions
ID | MCC-9 |
Title | Do not use MATLAB functions that are not supported by MATLAB Compiler. |
Priority | Strongly recommended |
Severity level | 1 |
Description | Do not use MATLAB-installed functions that are not supported by MATLAB Coder. |
Rationale | Either during compilation, or while running the code, errors are thrown, so avoid the functions as documented by The Mathworks. |