필터 지우기
필터 지우기

How do I change a copy of a cell array without changing the original cell array?

조회 수: 3 (최근 30일)
Robert Pastor
Robert Pastor 2021년 6월 28일
댓글: Rik 2021년 6월 29일
I have a 1x2 cell array of two collisionBox objects.
I want to create a copy of the cell array and then change the Pose matrices in the collisionBox objects.
However, when I change the copy, the original changes as well.
How to create a copy of a cell array that is not linked to the original?
ws_ = ws;
ws_{1}{1}.Pose(1,4) = -ws{1}{1}.Pose(1,4);
ws_{1}{1}.Pose(3,4) = -ws{1}{1}.Pose(3,4);
ws_{1}{2}.Pose(1,4) = -ws{1}{2}.Pose(1,4);
ws_{1}{2}.Pose(3,4) = -ws{1}{2}.Pose(3,4);
  댓글 수: 6
Robert Pastor
Robert Pastor 2021년 6월 29일
After learning that this is not cell array specific, but rather handle class specific, I found this workaround here
copyStream = getByteStreamFromArray(objToCopy);
objCopy = getArrayFromByteStream(copyStream);
Works for cel array as well.
Rik
Rik 2021년 6월 29일
This has indeed nothing to do with the cell. There are two fundamental kinds of variables: handle classes and data classes. A handle will only contain a pointer to the data itself, so copying the variable will only create a new pointer to the same data. Copying a data variable will create a copy of the data itself (although for memory efficiency Matlab uses a copy-on-write system where a variable is only actually duplicated when you change an element).

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Motion Planning에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by