MonkeyProof MATLAB Compiler Compatibility Standard

Version 1.3

cc4m_logo

© 2023 MonkeyProof Solutions BV

info@monkeyproofsolutions.nl

www.monkeyproofsolutions.nl

T: +31 (0)76 8200 314

Table of Contents

MonkeyProof MATLAB Compiler Compatibility Standard

Table of Contents

Introduction

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 MATLAB Coding Standard.

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:

  1. Install CC4M If you have not purchased the tool yet, you can do so here. Alternatively, you could request a free trial license there.
  2. 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.
  3. Select whether you want to check one or more files, all files in a folder, or all files in a MATLAB Project.
  4. Select what files/folder/project to check.
  5. Further customization is available, for example checking a folder including or excluding all subfolders.
  6. 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

IDA unique guideline identifier.
TitleBrief description of the guideline.
PriorityPriority 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.
DescriptionA more elaborate description of the guideline. Can include the reasoning behind the guideline: how does your code improve when applying the guideline?
RationaleOne 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

IDMCC-1
TitleAdapting the MATLAB and Java class path shall be avoided.
PriorityStrongly recommended
Severity level2
DescriptionRuntime changes to the MATLAB and Java class path shall be avoided.
RationaleIn 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

IDMCC-5
TitleDo not use scripts.
PriorityMandatory
Severity level3
DescriptionDo not use scripts because these are not supported by the MATLAB Compiler.
RationaleCompatibility, scripts are allowed in some use-cases, but not in others, see MathWorks documentation

Pragma required

IDMCC-7
TitleFunctions called from feval and eval constructions shall be included manually.
PriorityRecommended
Severity level6
DescriptionFunctions 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.
RationalePortability

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

IDMCC-8
TitleThe use of functions with (potential) different behaviour when deployed, shall be avoided.
PriorityRecommended
Severity level6
DescriptionThe use of functions with (potential) different behaviour when deployed, shall be avoided if possible. Otherwise their use needs manual review for documented reasons.
RationalePortability

manual review required

The following table shows the reviewing aspect.

FunctionRationale for reviewDoc
helpMATLAB file comments are stripped out before MATLAB Runtime encryption.
Note: actual behaviour seen in R2022b is a runtime error!
link
printdlgIn compiled mode, only one argument can be present in a call to printdlglink
whichIn compiled mode, which does not search the current folder.link
openIn compiled mode, open does not search the current folder.link
pwdWhen compiled, the start directory of an application is context-specific, so using pwd might result in unexpected values.
matlabrootAlternative 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.
isfileisfile(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.
isfolderisfolder(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

IDMCC-9
TitleDo not use MATLAB functions that are not supported by MATLAB Compiler.
PriorityStrongly recommended
Severity level1
DescriptionDo not use MATLAB-installed functions that are not supported by MATLAB Coder.
RationaleEither during compilation, or while running the code, errors are thrown, so avoid the functions as documented by The Mathworks.