Clear Filters
Clear Filters

creating a for loop

1 view (last 30 days)
Inti Vanmechelen
Inti Vanmechelen on 21 Dec 2015
Commented: Walter Roberson on 21 Dec 2015
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..

Accepted Answer

Walter Roberson
Walter Roberson on 21 Dec 2015
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 Comments
Inti Vanmechelen
Inti Vanmechelen on 21 Dec 2015
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 on 21 Dec 2015
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!