How does MATLAB handle memory for structures and cell arrays?

How does MATLAB handle memory for structures and cell arrays?

 Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 27 Jun. 2009

0 Stimmen

Structures and cell arrays in MATLAB are implemented as arrays of pointers to other arrays. When a field or cell is changed only the pointer value for that cell or field is changed.
To produce the correct behavior (as if MATLAB had copied the values) MATLAB creates a shared data link with the array you place in the field. If you change part of that field later with a statement such as
a.data(3) = 5;
MATLAB does create a copy at that time (if the other array still exists).
The sequence of statements
r = rand(512);
a.data = r;
r = [];
a.data(3) = 5;
does all the work in place with no copies. On the other hand, the statements
r = rand(512);
a.data = r;
a.data(3) = 5;
will make a copy on the last statement.
Note that if your structure or cell array is linked to another, changing one field will require that the pointer array be copied (but not the data).

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by