This week, guest blogger Jacob writes about how to use MATLAB-based tools to develop applications that are deployment-ready from inception.
Deployed applications rely on the MATLAB Compiler Runtime (the MCR) for execution. The MCR differs from MATLAB in subtle but important ways. You will find it much easier to deploy your MATLAB-based applications if you code with these differences in mind. You can learn how to develop deployable applications by reading our documentation and blog articles. To help you keep track of all the details, we're working on pushing some of this information into our development tools. One of first tools we enhanced for deployment is the MATLAB code analyzer, mlint.
The code analyzer examines MATLAB code to detect possible problems or improvements. The code analyzer is integrated into the MATLAB Editor so you can see its suggestions as you develop your code.
Code analyzer suggestions are divided into several categories (you can fine-tune which messages are shown through the Code Analyzer Preferences).
MATLAB Compiler (Deployment) Messages alert you when you use certain functions that may not deploy. However, you might not have noticed these messages before since this category is disabled by default. In order to enable this category of messages:
With Deployment messages enabled, the code analyzer can help detect when you are developing problematic code. For example, Writing Deployable Code used the following as an example of code that may compile but yield undesired results at run-time:
cd mathFcns z = add(x,y); cd ../stringFcns s = add(s1,s2);
In the MATLAB editor, the code analyzer highlights both instances of the cd command. By hovering your cursor over the underlined word, you view the warning message:
MCC use of the CD function is problematic.
Furthermore, if you click on the message, you get an explanation and suggested action. In this case, the suggested action is to avoid use of the CD command.
In fact, the Default Settings window in Code Analyzer Preferences displays all possible messages the code analyzer might display. Reading these messages helps you understand what code constructs the code analyzer is likely to catch.
While the code analyzer is a useful tool for detecting some instances of non-deployable code, it cannot detect all instances. For example, consider the following example which results in no warning or error message:
set(gca, 'ButtonDownFcn', 'LogButtonPress');
This code may result in a failure when deployed because MATLAB Compiler's dependency analysis (depfun) cannot detect that LogButtonPress is a function that must be included in the application. For this code to work in deployed mode, you must explicitly include LogButtonPress at compile-time (by using the -a switch in mcc). In this case, the code analyzer suffers from some of the same limitations that MATLAB Compiler's dependency analysis does---the inability to understand the contents of a string.
The code analyzer can help you to write deployable code, but it is not a substitute for understanding the compilation process or how running your application in MATLAB differs from running against the MCR. This understanding ultimately provides the best foundation for writing deployable code.
However, even if you cannot completely depend on the code analyzer to guarantee deployable code, you can still use it to quickly catch instances that might otherwise require lengthy debugging to correct. After all, problems caught while in development usually cost much less to fix than problems caught when the application is deployed.
Are there other MATLAB coding patterns for which you think the code analyzer should issue deployment warnings? Let us know here.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.