creating new fields in multiple structures with loops

3 次查看(过去 30 天)
Hi all
I have some data in the form of structures, eg strA, strB e.t.c. All structures have the same fields, let's say f1 and f2. I want to create the same new fields in every structure, for example:
strA.mean = mean(strA.f1);
strA.std = std(strA.f1);
Is there a way to do this with a loop so I don't have to write the above two lines of code for each structure? I tried the following but it doesn't work:
structnames = {'strA','strB','strC','strD','strF','strG','strK'}
n = length(structnames)
for i = 1:n
structnames{i}.mean = mean(structnames{i}.f1);
structnames{i}.std = std(structnames{i}.f1);
end
Any ideas?

采纳的回答

Ameer Hamza
Ameer Hamza 2020-3-16
It is never a good idea to name your variables like strA, strB, strC, ... There is a done of discussion on this topic explaining why it is an inefficient and dangerous: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. A better strategy is to create an array of struct accessible as str(1), str(2), str(3), ...
Although it is possible to do what you asked in your question, I will not be a part of this sin ?and only show you a way to create a struct array. After that, you can easily add a new field to all the fields to all the elements of the array.
strA.f1 = rand(1,10);
strA.f2 = rand(1,10);
strB.f1 = rand(1,10);
strB.f2 = rand(1,10);
% create the array of struct
clear str
vars = whos('str*');
str(numel(vars)) = struct('f1', [], 'f2', []);
for i=1:numel(vars)
str(i) = eval(vars(i).name);
end
% see how easy it is now
for i=1:numel(str)
str(i).mean = mean(str(i).f1);
str(i).var = var(str(i).f1);
end
  3 个评论
Ameer Hamza
Ameer Hamza 2020-3-17
It that case, it is still a better strategy to create struct arrays and save the name of the animals as a field of each struct. For example, try this.
lions.sex = {'M','M','F','F','F','M','F','F','M','F','F','F','M'};
lions.region = [1,1,1,1,1,1,2,2,2,2,2,2,3];
tigers.sex = {'F','M','F','M','F','M','F','F','F','M','F','M','F'};
tigers.region = [1,2,2,3,3,1,2,2,2,2,2,2,1];
% create the array of struct
vars = {'lions', 'tigers'};
clear animals
for i=1:numel(vars)
animals(i) = eval(vars{i});
end
% see how easy it is now
for i=1:numel(animals(i))
animals(i).name = vars{i};
animals(i).mean = mean(animals(i).region);
animals(i).var = var(animals(i).region);
end
Ameer Hamza
Ameer Hamza 2020-3-17
编辑:Ameer Hamza 2020-3-17
Now in case, it is absolutely necessary to keep the names of variables, and you have no other choice, then you can try this.
lions.sex = {'M','M','F','F','F','M','F','F','M','F','F','F','M'};
lions.region = [1,1,1,1,1,1,2,2,2,2,2,2,3];
tigers.sex = {'F','M','F','M','F','M','F','F','F','M','F','M','F'};
tigers.region = [1,2,2,3,3,1,2,2,2,2,2,2,1];
structnames = {'lions', 'tigers'};
for i=1:numel(structnames)
% create the actual command as a string
eval([structnames{i} '.mean = mean(' structnames{i} '.region);']);
eval([structnames{i} '.var = var(' structnames{i} '.region);']);
end
This will keep the variables names the same as before and add new fields, as shown inside the eval() function.

请先登录,再进行评论。

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2020-3-16
编辑:Fangjun Jiang 2020-3-16
This is the time to use structure array, not to use strA, strB, strC, ...
Just like use array A=1:3, not to use A1=1, A2=2, A3=3.
If you had strA, strB, strC, etc. from somewhere else, then you could do
str=[strA;strB;strC] and follow the code by @Ameer Hamza

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by