MATLAB Answers

Unsure why my for loop isnt working

2 views (last 30 days)
serena solanki
serena solanki on 7 Mar 2018
Commented: Bob Nbob on 7 Mar 2018
Hey All
I am unsure why my for loop isnt working
Essentially I have 6 sets of acceleration data, which I have opened and multiplied by a fixed mass to give me 6 sets of force data - labeled ft1,ft2 etc
I would like the loop to go through these 6 sets of forces
I have also interpolated the forces as my time step in the data is too large so I have made it smaller- and so need to interpolate my ft (force) values for the new time step t_interp to get my interpolated force ft_interp.
However when i run the code, I seem to be getting 0 values for my ft_interp.
I am unsure why it isnt running through each force value
Hope you can help!! Thanks in advance!! This is a snapshot of the loop
%fclose('all'); % Close the file Record_X.txt
m=773;% mass of structure in Mg
ft1 = m*acc1.';
ft2= m*acc2.';
ft3 = m*acc3.';
ft4 = m*acc4.';
ft5 = m*acc5.';
ft6 = m*acc6.';
ft7 = m*acc7.';
for ft=[ft1,ft2,ft3,ft4,ft5,ft6]
dt = 0.02; %time step of data (seconds)
NPTS = size(ft,2); %no.of points
tf = (NPTS-1)*dt;
t = 0:dt:tf;
dt_interp=0.005; %interpolating the data time step to increase no. of points
t_interp=dt_interp:dt_interp:tf;
ft_interp=F_interp(t,ft,t_interp); % interpolating the corresponding force values
NPTS_interp=size(t_interp,2);
end

  1 Comment

Jos (10584)
Jos (10584) on 7 Mar 2018
use the debugger and check every step

Sign in to comment.

Answers (1)

Bob Nbob
Bob Nbob on 7 Mar 2018
One of the problems you're going to have is in your number of points definition.
NPTS = size(ft,2);
Your for loop looks at all individual values of ft# so ft is always a single value, and NPTS results in 1. Follow that down and you have tf = 0 -> t = 0 -> all results taken at single time.
I'm assuming that you want to look at all values of a single ft# so I would suggest defining NPTS outside of the for loop, or adjusting the index of your for loop.
NPTS = size(ft1,2); % If they are all the same size
NPTS = size(ft{index},2); % If you want to loop through all acceleration files as a whole.
Side note, I would suggest putting all of your acceleration and force data into cell arrays. It makes it much easier to call them in a loop, and can allow you to define functions such as m*acc in a loop instead of individual equations.

  2 Comments

serena solanki
serena solanki on 7 Mar 2018
Hi Bob
Thanks for your help
I am using (index) in an internal loop- this outer loop is part of a nested loop. Is there an alternative that I can use so that it doesn't create a bug?
Bob Nbob
Bob Nbob on 7 Mar 2018
You can name your index whatever you would like, I just wrote out the word so you would understand what I was referring to. The purpose of this particular index though is for the cell array created by combining all of the ft# arrays into one cell array, which I called ft.

Sign in to comment.

Sign in to answer this question.