Simple Loop

2 views (last 30 days)
Thomas
Thomas on 3 Dec 2011
Hi, I'm OK with Matlab usually but this has me stumped. When running the following simple script I get the error
Attempted to access a_b(15); index must be a positive integer or logical.
Error in test (line 10) a_b(i) = a_a;
Here is the code:
ts = 0.01; % Timestep (s)
a_b = zeros(1,6001);
for t_a=0:ts:60
a_a = t_a*7;
i = t_a*(1/ts)+1; % Step
a_b(i) = a_a;
end
This is just the essentials of the loop that is causing the problem. Many thanks for any help in advance, Tom

Accepted Answer

Paulo Silva
Paulo Silva on 3 Dec 2011
ts = 0.01; % Timestep (s)
a_b = zeros(1,6001);
i=1;
for t_a=0:ts:60
a_a = t_a*7;
%i = t_a*(1/ts)+1; % Step
a_b(i) = a_a;
i=i+1;
end
plot(0:ts:60,a_b)
  1 Comment
Thomas
Thomas on 3 Dec 2011
oh yer, of course! Thanks very much

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!