If I have a struct
in Matlab with multiple fields defined with the same object, would that be treated as one object in memory or recreated for each field? In other words, I would like to have multiple field names for one object, is that possible in Matlab?
For example,
function output = fn(input)
output = struct('field1',input,'field2',input)
end
Would output
contain two copies of input
or one?
I recently found out a way to find an answer. One can type in the followings in the Matlab cmd.
A=magic(3);
S=struct('field1',A,'field2',A);
format debug
S.field1
S.field2
The output for me was
>> S.field1
ans =
Structure address = 369610c0
m = 3
n = 3
pr = 3fb54e40
pi = 0
8 1 6
3 5 7
4 9 2
>> S.field2
ans =
Structure address = 369610c0
m = 3
n = 3
pr = 3fb54e40
pi = 0
8 1 6
3 5 7
4 9 2
Note that the pr
values are the same for both fields.
I think pr
is the pointer to the real part of the matrix. So since both fields share the same pointer, there is no duplication of the same object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.