mecaeng
mecaeng

Reputation: 93

For loop for mean value?

How to code for loop to generate mean values of data form structure ?

 m1=mean(x1.data);
 m2=mean(x2.data);
 m3=mean(x3.data);
 m4=mean(x4.data);
 mi=mean(xi.data);

Upvotes: 2

Views: 1260

Answers (3)

Dan
Dan

Reputation: 45751

I think you can do it like this:

structfun(@mean, x1)

assuming you do as other Dan has suggested and made x and array of structs instead of having many separately named variables.

Upvotes: 1

Luis Mendo
Luis Mendo

Reputation: 112659

If you really need to use different variable names (instead of an array), you can use eval. It's not considered good practice though:

for n=1:10
  eval(['m' num2str(n) '=mean(x' num2str(n) '.data);']);
end

Upvotes: 2

Check mean. You can specify the dimensions.

Upvotes: 1

Related Questions