Expected one output from a curly brace or dot indexing expression, but there were 2 results, when re-initialize an object array

조회 수: 4 (최근 30일)
I am encountering this issue when I was working on clearing/re-initializing an object array.
A sample code snippet is here:
classdef classObj1
properties
a;
b;
c;
end
end
classdef classObj2
properties (Access = private)
obj1_arr;
end
methods
function addtoArr(obj, e)
obj.obj1_arr(end + 1) = e;
end
function clearArr(obj)
obj.obj1_arr = classObj1.empty(1, 0);
end
function setArr(obj, r)
obj.obj1_arr = r;
end
end
end
Everytime I create an classObj2, I will initialize its property, so the obj1_arr should be an 1d empty array of classObj1.
a = classObj2();
a.setArr(classObj1.empty(1, 0));
and add something to the array, everything is fine until here.
for i = 1:10
o = classObj1();
o.a = 1; o.b = 2; o.c = 3;
a.addtoArr(o)
end
However, when I try to call the clearArr() function, I get the following error message:
Expected one output from a curly brace or dot indexing expression, but there were 2 results.
on the line:
obj.obj1_arr = classObj1.empty(1, 0);
I have done my work on reading some posts for the reason and solution. e.g. using delete(), clear(), or obj.obj1_arr(:) = []; Unfortunately they don't work, and the identical error message is presented on the same line.
What I am trying to do is just clearing all elements in the obj1_arr since I would like to reuse it later without the data set in previous rounds, which is similar to list.clear() in python.
I appreciate any help that can assist me in solving this issue.

채택된 답변

Shubham Rawat
Shubham Rawat 2021년 3월 17일
Hi Cheng-Hung,
You had done spelling mistake inside the clearArr() function in the following line:
obj.obj1_arr = classObj1.empty(1, 0);
you had written classobj instead it is classObj.
Hope this Helps!
  댓글 수: 1
Cheng-Hung He
Cheng-Hung He 2021년 3월 19일
Hi Shubham,
Thanks for your answer! I have modifed the typo. Moreover, I found that the reason this error presented by Matlab is because the way I call the function.
Originally, I call it with
classObj2_arr.clearArr();
where the classObj2_arr is an array of classObj2, and this way incurs this error.
Instead, I should call it with a for loop:
for i = 1:lenth(classObj2_arr)
classObj2_arr(i).clearArr();
end
Though I have no idea why the first approach cannot be used, this change solves the bug.:)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by