Updating structure data within a containers.map.

조회 수: 6 (최근 30일)
Nathan Bhoedjang
Nathan Bhoedjang 2020년 4월 20일
댓글: teeeeee 2020년 5월 13일
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

채택된 답변

Walter Roberson
Walter Roberson 2020년 4월 20일
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.
  댓글 수: 2
Nathan Bhoedjang
Nathan Bhoedjang 2020년 4월 20일
Great thanks! I will try your other suggestion as well.
teeeeee
teeeeee 2020년 5월 13일
@Walter can you show a very basic example of what you said: "creating a nonscalar structure and indexing into it", (or alternatively just point me to an example of this somewhere in the docs?).
I am trying to have some varying types of data stored as a map such that I can access attributes nicely by a label (character vector), without having to keep track of integer indexing. Something like the following:
keys = {'Jan','Feb','Mar'};
data_map = containers.Map('KeyType','char','ValueType','any');
% loop over keys and put some data in the map
for k = 1:length(keys)
data_struct.prices = rand(1,5); % create some dummy numbers
data_struct.description = 'HIGH'; % create a dummy label
% place the structure in the map at the relevant key
data_map(keys{k}) = data_struct;
% delete ready for next iteration of the loop
clear data_struct
end
% a nice way to access the data (no integer indexing required, only the key)
disp( data_map('Feb').description )
However, sometime later, when I come to add some extra data, such as the following I get an error:
% data_map('Feb').temperature = rand(1,8); % gives Error
Instead, I find I have to do something like the following:
% do it instead with a temporary structure
temp_struct = data_map('Feb');
temp_struct.temperature = rand(1,8);
data_map('Feb') = temp_struct;
clear temp_struct
disp( data_map('Feb').temperature )
Is this really the best way to do this, or were you hinting in your previous answer that there is a nicer way?
Thank you!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by