Assuming you would be okay with having a single struct that will hold the three structs named 'dark','oa_45', 'oa_225' and the field 'sqrtmean' being dropped down to the bottom, you might wanna try this -
Code Version 1 (Recommended)
A= {'dark';'oa_45'; 'oa_225'};
fieldnames1 = {'count';'mean';'stdev'};
funcnames1 = {'';'mean';'std'};
for k = 1:numel(A)
tmp =load([folder '/' A{k} '.txt']);
struct1 = A{k};
for i = 1:numel(fieldnames1)
if isempty(funcnames1{i})
comp_struct.(struct1).count=tmp(:,1:2);
else
fh = str2func(funcnames1{i});
comp_struct.(struct1).(fieldnames1{i}) = fh(tmp(:,1:2),1);
end
end
comp_struct.(struct1).sqrtmean = sqrt(comp_struct.(struct1).mean);
end
Thus, 'comp_struct.dark' would be your original 'dark' and so on.
Go one step further and remove the IF-ELSE -
Code Version 2 (Highly Recommended)
A= {'dark';'oa_45'; 'oa_225'};
fieldnames1 = {'count';'mean';'stdev'};
funcnames1 = {'donothing';'mean';'std'};
for k = 1:numel(A)
tmp =load([folder '/' A{k} '.txt']);
struct1 = A{k};
for i = 1:numel(fieldnames1)
fh = str2func(funcnames1{i});
comp_struct.(struct1).(fieldnames1{i}) = fh(tmp(:,1:2),1);
end
comp_struct.(struct1).sqrtmean = sqrt(comp_struct.(struct1).mean);
end
Don't forget to add this function in path -
function out = donothing(varargin)
out = varargin{1};
If the nested loops are bothering you or you don't want to get into function handles and you were only looking to replace EVAL with something that would occupy the same space in terms of code lines, use the the following, but I won't recommend for a general case though -
Code Version 3 (Not Recommended)
A= {'dark';'oa_45'; 'oa_225'};
for k = 1:numel(A)
tmp =load([folder '/' A{k} '.txt']);
comp_struct.(A{k}).count = tmp(:,1:2);
comp_struct.(A{k}).mean = mean(tmp(:,1:2),1);
comp_struct.(A{k}).sqrtmean = sqrt(comp_struct.(A{k}).mean);
comp_struct.(A{k}).stdev = std(tmp(:,1:2),1);
end