How do I run static analysis on custom classes?

2 次查看(过去 30 天)
Is there a mechanism in MATLAB to get it to run a static analysis/linter to ensure that my classes are being accessed correctly (no typos, undefined fields,...)? For example, say I have myclass.m:
classdef myclass
properties
prop (1,1) int16 {mustBePositive} = 1
end
methods
% ...
end
end
and script.m:
test = myclass;
test.prop = 2; % is fine
test.porp = 2; % error, 'prop' is misspelled
test.name = 'test'; % error, no property named 'name'
test.prop = 1.1; % error, 'prop' is an integer
test.prop = [1 2]; % error, 'prop' must have size (1,1)
The errors only show up once I run the code and not while I am editing. Is there a linter or something similar to what I get in C/C++ (in vscode for example) that can give me these errors without running everything? At the very least I'd like to be able to identify the first two issues (which are really the same issue) of trying to access a property that doesn't exist.

回答(1 个)

Subhajyoti
Subhajyoti 2024-8-29
When you are typing in the MATLAB Editor, there are handy suggestions pop-up for objects, functions, variables, etc. by default. You can refer to the following link to know more about ‘Customize Code Suggestions and Completions’ in MATLAB:
Also, you can run MATLAB Code in Visual Studio Code (VS Code) with features such as syntax highlighting, code analysis, navigation support, and more using the ‘MATLAB’ extension.
You can refer to the following link to know more about ‘Run MATLAB in Visual Studio Code’:
Also, the following is a helpful resource you can refer to ‘Avoiding Repetitive Typing with Tab Completion’ in MATLAB:
I hope this helps.
  1 个评论
Benjamin Cerjan
Benjamin Cerjan 2024-8-29
Sure, but what if I edit the class after I write the script? Or if I ignore/miss the completions it doesn't flag test.name as an error (even though it should know that isn't a valid property?
I have a project where I recently altered one of the class used in several different places and it is tedious and error-prone to try and go through everything by hand looking for places where old names are referenced.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by