How to get my for loop working?
2 views (last 30 days)
Show older comments
Inti Vanmechelen
on 22 Dec 2015
Edited: Walter Roberson
on 22 Dec 2015
Hi,
I want to fill an empty matrix with values that equal the size of other matrices, being muscles. I tried looping over the different muscles, but the loop gives me a matrix with 6 times the size of last muscle, instead of 6 different numbers.
Here's my code:
Names2 = {'soleus', 'tibant', 'gaslat', 'vaslat', 'rectfem', 'hamlat'};
Vars2 = {soleus, tibant, gaslat, vaslat, rectfem, hamlat};
nvars = length(Vars2);
Duration = zeros(6,3);
for s = 1 : nvars
for d = 1:6
Duration(d,1) = length(IndexMuscleActGait.(names2{s}));
end
end
This gives me a matrix with in the first colum 6 times the number 50, which equals length of IndexMuscleActGait of the last muscle. However, I want the length of the 6 different muscles, and I can't find the right command to get it working.
0 Comments
Accepted Answer
Walter Roberson
on 22 Dec 2015
for s = 1 : nvars
Duration(s,1) = length(IndexMuscleActGait.(names2{s}));
end
2 Comments
Renato Agurto
on 22 Dec 2015
for s = 1 : nvars
Duration2(s,1) = length(IndexMuscleActGait.(names2{s}));
Duration2(s,2) = length(IndexMuscleActStairUp.(names2{s}));
Duration2(s,3) = length(IndexMuscleActStairDown.(names2{s}));
end
or
for s = 1 : nvars
Duration2(s,1:3) = [length(IndexMuscleActGait.(names2{s})),...
length(IndexMuscleActStairUp.(names2{s})),...
length(IndexMuscleActStairDown.(names2{s}))];
end
More Answers (1)
Renato Agurto
on 22 Dec 2015
Edited: Renato Agurto
on 22 Dec 2015
I think you should use
Duration(d,s) = ... %not Duration(d,1)
or maybe what you want is
Duration(d,1) = length(IndexMuscleActGait.(names2{d}));
if this last is correct, why do you have 2 for loops?
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!