Rand in the constructor of a class and createArray
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm attempting to use createArray to create an array of a class. The class constructor sets a property using rand. When I use createArray, every object in the array gets the same value of that property. Is there a workaround?
댓글 수: 0
답변 (2개)
Andres Adam
2024년 7월 15일
편집: Walter Roberson
2024년 7월 15일
It seems that createArray is not creating a new object for each element, but rather creating one and copying on the rest of the elements.
Maybe a workaround is to specify a copy method for your class, so the property changes with rand even when it is copied. Have you tried that?
댓글 수: 0
Walter Roberson
2024년 7월 15일
When you have static properties of the class, then the value that is assigned when the class is loaded is used for all subsequent members of the class. To change the value, you need to clear the class.
In short... you should not have assigned a per-instance property at the class level.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!