Updating structure data within a containers.map.

6 vues (au cours des 30 derniers jours)
Nathan Bhoedjang
Nathan Bhoedjang le 20 Avr 2020
Commenté : teeeeee le 13 Mai 2020
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

Réponse acceptée

Walter Roberson
Walter Roberson le 20 Avr 2020
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 commentaires
Nathan Bhoedjang
Nathan Bhoedjang le 20 Avr 2020
Great thanks! I will try your other suggestion as well.
teeeeee
teeeeee le 13 Mai 2020
@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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Scripts dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by