creating a for loop

1 次查看(过去 30 天)
I Know it is probably a really stupid question, but I've been working on this thing for a couple houres and I can't get it to work..
I calculated the means and standardeviations for 6 muscles in 3 motions (Gait, StairUp, StairDown) and my intention is to create 3 structures, respectively: Gait, StairUp and Stairdown, containing the mans and std's of the 6 muscles.
Gait.soleus(:,2) = std(soleus.Gait,0,2);
Gait.tibant(:,2) = std(tibant.Gait,0,2);
Gait.gaslat(:,2) = std(gaslat.Gait,0,2);
Gait.vaslat(:,2) = std(vaslat.Gait,0,2);
Gait.rectfem(:,2) = std(rectfem.Gait,0,2);
Gait.hamlat(:,2) = std(hamlat.Gait,0,2);
StairUp.soleus(:,2) = std(soleus.StairUp,0,2);
StairUp.tibant(:,2) = std(tibant.StairUp,0,2);
StairUp.gaslat(:,2) = std(gaslat.StairUp,0,2);
StairUp.vaslat(:,2) = std(vaslat.StairUp,0,2);
StairUp.rectfem(:,2) = std(rectfem.StairUp,0,2);
StairUp.hamlat(:,2) = std(hamlat.StairUp,0,2);
StairDown.soleus(:,2) = std(soleus.StairDown,0,2);
StairDown.tibant(:,2) = std(tibant.StairDown,0,2);
StairDown.gaslat(:,2) = std(gaslat.StairDown,0,2);
StairDown.vaslat(:,2) = std(vaslat.StairDown,0,2);
StairDown.rectfem(:,2) = std(rectfem.StairDown,0,2);
StairDown.hamlat(:,2) = std(hamlat.StairDown,0,2);
Gait.soleus(:,1) = mean(soleus.Gait,2);
Gait.tibant(:,1) = mean(tibant.Gait,2);
Gait.gaslat(:,1) = mean(gaslat.Gait,2);
Gait.vaslat(:,1) = mean(vaslat.Gait,2);
Gait.rectfem(:,1) = mean(rectfem.Gait,2);
Gait.hamlat(:,1) = mean(hamlat.Gait,2);
StairUp.soleus(:,1) = mean(soleus.StairUp,2);
StairUp.tibant(:,1) = mean(tibant.StairUp,2);
StairUp.gaslat(:,1) = mean(gaslat.StairUp,2);
StairUp.vaslat(:,1) = mean(vaslat.StairUp,2);
StairUp.rectfem(:,1) = mean(rectfem.StairUp,2);
StairUp.hamlat(:,1) = mean(hamlat.StairUp,2);
StairDown.soleus(:,1) = mean(soleus.StairDown,2);
StairDown.tibant(:,1) = mean(tibant.StairDown,2);
StairDown.gaslat(:,1) = mean(gaslat.StairDown,2);
StairDown.vaslat(:,1) = mean(vaslat.StairDown,2);
StairDown.rectfem(:,1) = mean(rectfem.StairDown,2);
StairDown.hamlat(:,1) = mean(hamlat.StairDown,2);
Obviously, this needs to go in a for loop to be more efficient, but If I try loop over the different muscles with:
Names2 = {'soleus' 'tibant' 'gaslat' 'vaslat' 'rectfem' 'hamlat'};
format = size(Names2);
for s = 1:1:format(2);
For the first element this would give:
Gait.(names2{s})(:,2) = std((names2{s}).Gait,0,2);
But matlab gives a red square saying: "invalid use of ')'" before I run anything..

采纳的回答

Walter Roberson
Walter Roberson 2015-12-21
Names2 = {'soleus' 'tibant' 'gaslat' 'vaslat' 'rectfem' 'hamlat'};
Vars2 = {soleus, tibant, gaslat, vaslat, rectfem, hamlat};
nvars = length(Vars2);
for s = 1 : nvars
Gait.(names2{s})(:,2) = std(Vars2{s}.Gait,0,2);
end
  2 个评论
Inti Vanmechelen
Inti Vanmechelen 2015-12-21
That worked, thank you!
Could it maybe possible to explain why I should work with the length of vars2? So I can understand a bit more what I am doing.
Walter Roberson
Walter Roberson 2015-12-21
length(Vars2) is the same as length(Names2) . Because we constructed both of them using a row vector, the length corresponds to the second dimension, same as size(Names2,2). length() is easier to write.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by