Matlab class property specification

2 ビュー (過去 30 日間)
XGQ
XGQ 2022 年 3 月 6 日
コメント済み: XGQ 2022 年 3 月 10 日
classdef MAtWarning
properties
P@uint8=uint8(1); % undocumented way
% P(1,1)uint8=uint8(1); % R2021b style
end
methods(Static=true)
function Test
clear;clc;
Instance=MAtWarning;
aa=Instance.P;
fprintf(2,'%u...\n',aa(1));
end
end
end
I'm migrating a project from R2016a to R2021b. Many classes use the undocumented way to specify properties.
The undocumented way gets warning in R2021b, and the R2021b style is obviously better, but I need time to see if all the code works fine at R2021b, so I hope there is a way for the code to work fine both in R2016a & R2021b, so that I could switch the MCR whenever I want.
Suppress the warning does not seems like a perfect solution to me.
warning('off','MATLAB:class:PropUsingAtSyntax');
Hope someone can help me.

採用された回答

Yair Altman
Yair Altman 2022 年 3 月 10 日
Starting in 16a, you can use the new syntax as long as you don't specify the dimensions. In other words, the following 2 propery definitions are essentially equivalent:
classdef test
properties
prop1@uint8 = uint8(1); % undocumented way
prop2 uint8 = uint8(1); % documented way
end
end
Note that the documented dimentionality specification (e.g. (1,1) instead of scalar) requires a more recent release - it will not run on 16a. But as long as you don't specify dimentionality and as long as you are sure that the code will not be ran in 15b or earlier, then it should be perfectly safe to simply remove the @ symbol in all your class property definitions, and this code will work on any relase since 16a.
Additional information and comparison between the documented/undocumented mechanisms: https://undocumentedmatlab.com/articles/setting-class-property-types-2
  1 件のコメント
XGQ
XGQ 2022 年 3 月 10 日
It works!
Thank you so much! Not only for this answer, but also for all the things learned from your amazing website all these years, helped me a lot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Find more on Graphics Object Programming in Help Center and File Exchange

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by