"Terrell" wrote in message <firstname.lastname@example.org>... > "Terrell" wrote in message <email@example.com>... > > I am trying to make a for if loop work in matlab but am having no luck. It is as if the for loop runs once and then stops. The code is as follows: > > > > fs=4000; > > Ts=1/fs; > > N1=241; > > tb=.055; > > te=.115; > > Ta=.060; > > T=.050; > > A=10; > > fc=1000; > > theta=0; > > for t=-1:.001:1; > > if (T<=t) && (t>=0) > > g2=A*cos(2*pi*fc*t+theta); > > else > > g2=0; > > end > > if (Ta<=t) && (t>=(T+Ta)) > > g1=A*cos(2*pi*fc*(t-Ta)+theta); > > else > > g1=0; > > end > > end > > > > No matter what I do I end up with a 1 for t and 10 for g1 and g2. I can't seem to figure out how to get past this. Any help is appreciated, > > I guess I should have been more clear also. At the completion of the loop I need to have a matrix for t values and a matrix for the functions g1 and g2. The original purpose of the for if loop was to apply a multiplication by a shifted rect function. But I definately see the points you are both making about overwriting the t an g1 and g2 values. Should I use some sort of indexing to build a matrix via a loop or is there an easier way to do all of this?
dbp showed you how do do it "the right way." A quick & dirty method would simply be to add indx = 1 before the loop, replace your g1 & g2 in the loop with g1(indx) & g2(indx) and add indx = indx + 1 at the end of (within) the loop.
To be more efficient, you should preallocate g1 & g2 before the loop.