Is there a document describing MATLAB coding standards or a good programming guide available?

I am looking for documents that can help improve my MATLAB programming skill. I would be interested in any helpful online guides.

 Respuesta aceptada

Coding Standards
As of 2025, MathWorks has a published set of recommended MATLAB Coding Guidelines which can be found in the GitHub link to MATLAB Coding Guidelines. These guidelines are designed to help large teams work together better by using consistent approaches to writing good MATLAB code. These include recommendations on the number of input variables for your function, variable names, casing rules for classes, among other things. They even ship with a 'codeAnalyzerConfiguration.json' to automatically apply all of the rules that can be detected by Code Analyzer. 
You could also use the MonkeyProof Coding Standard for MATLAB. This standard is maintained by a Change Control Board (CCB) and has been developed in collaboration with industry leaders and the TIOBE organization, which specializes in coding standards and code quality.
Best Practices for MATLAB Coding
You can refer to the  MATLAB Code Analyzer Report, which helps in improving code through warning and error messages. For additional best practices in MATLAB coding, explore Loren on the Art of MATLAB.
To further enhance performance and memory usage in MATLAB scripts, consider reviewing these documentation pages:
For more help on specific functions or workflows, please check out our documentation Help Center and MATLAB Answers

Más respuestas (1)

Co Melissant
Co Melissant el 5 de Oct. de 2022
Editada: MathWorks Support Team el 6 de Dic. de 2024
Since January 2022 we have the MonkeyProof MATLAB Coding Standard, see https://doc.monkeyproofsolutions.nl/code-checker-for-matlab/monkeyproof-coding-standard-for-matlab/
This standard has a Change Control Board (CCB) and is written together with some industry-leaders and TIOBE https://www.tiobe.com, a company specialized in Coding Standards and Code Quality

5 comentarios

The monkeyproof coding standard is quite detailed, definitely worth a read.
Although this is a moving goal, it would be nice to include a section on preferred vs deprecated functions, e.g.
  • use DATETIME, avoid DATENUM, DATESTR, DATEVEC.
  • use of strings and JOIN, COMPOSE etc.
  • use of HISTCOUNTS (noting the difference in the upper edge compared to HISTC).
  • use of tables/timetables (and all the related file import options rather than post-processing after import).
  • etc.
Hi Stephen,
Thanks, these are all good ideas! We take these with us on the next CCB meeting. If you like to join the meeting to explain and discuss this and more, contact us!
With this Standard we also have a code checker (CC4M, https://monkeyproofsolutions.nl/products/code-checker-for-matlab/). CC4M checks many rules from the standard, and offers more checks. Hereby some related to your ideas:
  • Check for use of strings or character arrays. Both datatypes can be reported as a violation. If you have a code base that needs to support pre-string MATLAB versions you may want to report strings as a violation. Code generation support for strings in Simulink was introduced much later then R2016b/R2017a timeframe (introduction of strings)
  • Check against release compatibility. CC4M checks if your code will run in the release you want it to run as a minimum. ( https://doc.monkeyproofsolutions.nl/code-checker-for-matlab/code-checker-for-matlab/checks.html - just see we hardcoded the R2021a in the doc, this should be latest release). We do have the same approach for checking your code against compatibility for the MATLAB Coder.
I do not agree that character vectors should be considered deprecated. string operations are slower in most cases (with the notable exception that double() of a string is faster than any of the known alternatives.)
Example for generating random words with two letters, then two digits, then a letter:
azc = ('a':'z').';
dc = ('0':'9').';
azs = string(azc);
ds = string(dc);
N = 10000;
RL = randi(26, N, 3);
RD = randi(10, N, 2);
tic; outc = [azc(RL(:,1:2)), dc(RD), azc(RL(:,3))]; toc
Elapsed time is 0.000732 seconds.
tic; outs = azs(RL(:,1)) + azs(RL(:,2)) + ds(RD(:,1)) + ds(RD(:,2)) + azs(RL(:,3)); toc
Elapsed time is 0.002103 seconds.
tic; outs2 = join([azs(RL(:,1:2)), ds(RD), azs(RL(:,3))], ""); toc
Elapsed time is 0.002738 seconds.
outc(1:5,:)
ans = 5×5 char array
'aw45w' 'zd63h' 'rl77q' 'gi40v' 'hb20p'
outs(1:5)
ans = 5×1 string array
"aw45w" "zd63h" "rl77q" "gi40v" "hb20p"
outs2(1:5)
ans = 5×1 string array
"aw45w" "zd63h" "rl77q" "gi40v" "hb20p"
There can be various reasons to go for char or for string to handle text, I mentioned a few as well. This is why CC4M can check on both. Also there is currently is no rule in the standard. But a rule is there to have exemptions on it :)
"I do not agree that character vectors should be considered deprecated."
I do not consider CHAR arrays as deprecated.
I do note that many users these days seems to expect text to be "atomic", perhaps due to experience with other programming languages and environments. SPRINTF et al processing arrays in memory-order produces countless threads on this forum... in contrast, overloaded PLUS (and COMPOSE, etc.) intuitively match what many users see and expect. For many common text tasks, it seems STRING is simpler.
It is also clear that the behavior of code can be very very different if users are not paying attention to these types and writing their code taking their differing properties into account:
I presumed that one point of an automated coding check is to warn for these kind of compatibility issues.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by