Why does resetting a 0x0 cell property value result in 0x1 cell?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Steven H
am 7 Jul. 2021
Beantwortet: Steven H
am 7 Jul. 2021
I unfortunately cannot share the code, because it's confidential. I have tried to create a MWE, but it appears not to recreate the issue, which confuses me even more. I'm using MATLAB R2017b.
I have a class definition with a property that has as default value an empty cell, {}, which is more specifically a 0x0 empty cell array. The class has a reset method that simply resets the property value to the empty cell {}. However, if I create an object of the class and reset the object, the property value is set to the empty cell, good, but more specifically a 0×1 empty cell array, not good. If I now run unit testing functions, it tests whether two class objects are equal after one is reset, which is not true because isequal({}, cell(0,1)) returns false. This is the real problem. Also, it displays the value as {0×1 cell} instead of just {} which is plainly annoying.
As I said, I cannot recreate the issue in a MWE. I have tried searching MATLAB Answers, found nothing.
EDIT: I will still include the MWE so it's clear exactly what the workflow would be. Imagine the class definition is as follows:
classdef MyClass < handle
properties
Property1 = {};
end
methods
function obj = MyClass
end
function reset(obj)
obj.Property1 = {};
end
end
end
Then, I would run the code
obj = MyClass;
obj
obj.reset
obj
Then the output on the command window would be
obj =
MyClass with properties:
Property1: {}
obj =
MyClass with properties:
Property1: {0x1 cell}
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Jan
am 7 Jul. 2021
If we cannot see the reset function, there is no chance, that we can guess, how the {0x1} cell is created.
You could add a test for empty cells, which set all empty cells to {0x0}:
if isempty(c)
c = {};
end
Siehe auch
Kategorien
Mehr zu Software Development Tools finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!