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?
댓글 수: 0
채택된 답변
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
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
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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!