Why does resetting a 0x0 cell property value result in 0x1 cell?

9 ビュー (過去 30 日間)
Steven H
Steven H 2021 年 7 月 7 日
回答済み: Steven H 2021 年 7 月 7 日
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}

採用された回答

Steven H
Steven H 2021 年 7 月 7 日
Turns out, there is a setter that forces the input to be a column vector by doing e.g.
c = {1, 2};
c(:)
To complete the MWE, add the set method
function set.Property1(obj, in)
obj.Property1 = in(:);
end
and that's how the issue can be recreated. The solution is to check whether the input is empty or not before setting it like that.

その他の回答 (1 件)

Jan
Jan 2021 年 7 月 7 日
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
  1 件のコメント
Steven H
Steven H 2021 年 7 月 7 日
As I said, the issue is not recreated in a MWE so I don't know how that would be helpful. Though of course I understand that the only thing one can do is guess. I was hoping it's a known MATLAB bug to some people. For clarification, I'll add the MWE still.

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

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by