Is there a document describing MATLAB coding standards or a good programming guide available?
Mostrar comentarios más antiguos
I am looking for documents that can help improve my MATLAB programming skill. I would be interested in any helpful online guides.
Respuesta aceptada
Más respuestas (1)
Co Melissant
el 5 de Oct. de 2022
Editada: MathWorks Support Team
el 6 de Dic. de 2024
6 votos
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
Stephen23
el 13 de Abr. de 2023
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.
This might be useful: https://www.mathworks.com/matlabcentral/answers/492752-table-of-matlab-release-features
Co Melissant
el 14 de Abr. de 2023
Editada: MathWorks Support Team
el 10 de Oct. de 2024
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.
Walter Roberson
el 14 de Abr. de 2023
Editada: Walter Roberson
el 14 de Abr. de 2023
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
tic; outs = azs(RL(:,1)) + azs(RL(:,2)) + ds(RD(:,1)) + ds(RD(:,2)) + azs(RL(:,3)); toc
tic; outs2 = join([azs(RL(:,1:2)), ds(RD), azs(RL(:,3))], ""); toc
outc(1:5,:)
outs(1:5)
outs2(1:5)
Co Melissant
el 14 de Abr. de 2023
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.
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!