MATLAB: Updating structure data within a containers.map.

dataMATLABstructures

Hi, I just got started on a pred-prey simulation. I started out with structures two preys and I put them in a map. Now I want to make their age (starting with the prey's) increase over time. When I run my code I get the following error in the second for loop: only one level of indexing is supported by a containers.Map. Even when I try to change the age outside of the loop I get the same error. Can anyone help me out? I'm new to MATLAB…
numDays = 100;
preyMap = containers.Map('KeyType', 'double', 'ValueType', 'any');
prey1 = struct('x', 2, 'y', 2, 'age', 365, 'lastbirth', 100);
preyMap(1) = prey1;
prey2 = struct('x', 3, 'y', 3, 'age', 365, 'lastbirth', 100);
preyMap(2) = prey2;
for p = 1:length(preyMap)
disp(preyMap(p).age)
for t = [1:numDays]
preyMap(p).age = preyMap(p).age + t
disp(preyMap(p).age)
end
end

Best Answer

for p = 1:length(preyMap)
disp(preyMap(p).age)
for t = [1:numDays]
tp = preyMap(p);
tp.age = tp.age + t;
preyMap(p) = tp;
disp(preyMap(p).age)
end
end
I would suggest, however, that putting a structure inside a map is not as efficient as just creating a nonscalar structure and indexing into it.