Matlab Implicit default values are broken when using property validation functions for properties that store handle objects.

14 visualizzazioni (ultimi 30 giorni)
EDIT: I submitted a bug report. I will update this post as soon as I have new information.
Hi,
I have a class that has a property of type containers.Map. If I add property validation functions I can't assign default values in the constructor without having an initial value first (otherwise matlab assigns a default value of type double, which throws an error because it is the wrong type). The initial value however - when explicitly stated - triggers matlab to issue a warning because the map object would be shared across all instances of the class if I don't overwrite it.
Here is a small example:
classdef matlabBroke
properties
map (:, 1) {mustBeA(map, 'containers.Map')};
end
methods
function obj = matlabBroke()
obj.map = containers.Map();
end
end
end
This code doesn't run. It throws the error message:
Error using implicit default value of property 'map' of class 'matlabBroke'. Value must be one of the following types:
'containers.Map'.
It can be shrunken down even more:
classdef matlabBroke
properties
map {mustBeA(map, 'containers.Map')};
end
end
or even:
classdef matlabBroke
properties
map containers.Map;
end
end
It only runs when I init the property first:
classdef matlabKindaBroke
properties
map (:, 1) {mustBeA(map, 'containers.Map')} = containers.Map();
end
methods
function obj = matlabKindaBroke()
obj.map = containers.Map();
end
end
end
This however triggers a warning regarding the shared map object across all instances of this class.
A property default value that is a handle will cause all instances to share the same object data. [...]
There are two flaws:
  • the error shouldn't trigger in this case
  • the default values aren't immediately obvious although there's a clearly defined place where default values should be when using property validation (https://de.mathworks.com/help/matlab/matlab_oop/validate-property-values.html, first image).
Additionally, initializing objects in the constructor makes it way more difficult to spot the default value, especially in complex classes.
A solution I came up with (would have to be implemented by mathworks):
add the keyword 'new' to matlab. The first example
classdef matlabBroke
properties
map (:, 1) {mustBeA(map, 'containers.Map')};
end
methods
function obj = matlabBroke()
obj.map = containers.Map();
end
end
end
could look like this:
classdef matlabBroke
properties
map (:, 1) containers.Map = new containers.Map();
end
end
This should trigger the default value to be regenerated every time a class is instantiated.
This could omit the error and the default value is in the same place as the property definition. Additionaly, the word 'new' highlights that this handle is not shared across all objects of this class.
If anybody has an easier way to do this please let me know!
My solution right now is to ignore the error and add the default value in the constructor.

Risposte (1)

Shivam
Shivam il 9 Ott 2023
Modificato: Shivam il 9 Ott 2023
Hi Jakob,
As per my understanding, you are getting a warning while creating a property with validators in the class and want to know if the instance can be created using the 'new' keyword.
According to the documentation, in MATLAB, the keyword 'new' is not used for creating objects of a class. You can create properties in the class with validators and default Values and use the constructor to initialize the property with an empty 'containers.Map'.
You can refer to the following documentations to know more about property syntax and initialization:
  1. https://www.mathworks.com/help/releases/R2023a/matlab/matlab_oop/defining-properties.html
  2. https://www.mathworks.com/help/matlab/matlab_oop/initialize-property-values.html
I hope it helps.

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by