Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
erratic behaviour from 'for' loops, counter not changing

Subject: erratic behaviour from 'for' loops, counter not changing

From: Paddy Carter

Date: 21 Oct, 2010 17:08:03

Message: 1 of 5

apologies for the imprecision of a non-programmer.

my basic problem is that I run loops that look like this:

for nt=1:4;
for up=1:4;
...
end
end

and the thing runs 16 times, but for some reason keeps the value of one (some times both) of the counters at 1. And the weird thing is, this is a new problem that's started happening with routines I have been using for many months, and it is intermittent. For example, sometimes if I change the index to nt=1:3; it will work, other times, not.

I can't post the full code (too long) and appreciate this is probably too vague to get a precise answer, what I'm hoping is that somebody might have had a similar experience and have got to the bottom of it.

Basically I am running a simulation using the loop merely to pick out different parameter values for the simulation, and to store the results in corresponding arrays. The counters nt and up are in an m.file called globalpar.m, which just declares them as global. My results storage arrays look like Allresults(nt,up)=Z

I know this probably falls short of the information people are going to need but I don't know what else would be useful to provide.

thanks for any help
 

Subject: erratic behaviour from 'for' loops, counter not changing

From: Sean

Date: 21 Oct, 2010 17:26:04

Message: 2 of 5

> Basically I am running a simulation using the loop merely to pick out different parameter values for the simulation, and to store the results in corresponding arrays. The counters nt and up are in an m.file called globalpar.m, which just declares them as global. My results storage arrays look like Allresults(nt,up)=Z

That's probably your problem. Globals are bad.
http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_are_global_variables_bad.3F

Subject: erratic behaviour from 'for' loops, counter not changing

From: Steven_Lord

Date: 21 Oct, 2010 17:27:26

Message: 3 of 5



"Paddy Carter" <paddy.carter.remove@bristol.ac.uk> wrote in message
news:i9ps1j$rpd$1@fred.mathworks.com...
> apologies for the imprecision of a non-programmer.
>
> my basic problem is that I run loops that look like this:
>
> for nt=1:4;
> for up=1:4;
> ...
> end
> end
>
> and the thing runs 16 times, but for some reason keeps the value of one
> (some times both) of the counters at 1.

Well, nt will have the value 1 four times, and so will up. First nt will be
1 and up will be 1, then nt will remain at 1 and up will become 2. Next nt
stays at the value 1 and up changes to 3, and then nt is 1 and up is 4.

At the next iteration of the outer loop, nt becomes 2 and up starts
iterating through 1:4 again; the first value it takes on is 1. Then nt = 2
and up = 2, nt = 2 and up = 3, etc.

> And the weird thing is, this is a new problem that's started happening
> with routines I have been using for many months, and it is intermittent.
> For example, sometimes if I change the index to nt=1:3; it will work,
> other times, not.
> I can't post the full code (too long) and appreciate this is probably too
> vague to get a precise answer, what I'm hoping is that somebody might have
> had a similar experience and have got to the bottom of it.
> Basically I am running a simulation using the loop merely to pick out
> different parameter values for the simulation, and to store the results in
> corresponding arrays. The counters nt and up are in an m.file called
> globalpar.m, which just declares them as global.

Don't do this. You're giving any function with access to the global
workspace the ability to interact with your loop variables. I strongly
suggest you use _local_ variables, not global, as your loop indices.

*snip*

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: erratic behaviour from 'for' loops, counter not changing

From: Paddy Carter

Date: 21 Oct, 2010 18:55:08

Message: 4 of 5

Thanks Steve ... I have come across the notion globals are bad before. The link you send me to suggests they are primarily a problem because you can get too many of them and lose control ... that definitely a problem in my case I have about 5, and also, regretably, the advice "collecting your globals into a struct (i.e., cleaning up your toolbox) and passing it on as a parameter of your functions is the most economic way to program in MATLAB " is beyond my comprehension. I am using functions that somebody else has written.

however if somebody can explain to me how I can do simple 'for' loops and use the counter to pick out parameters and storage addresses, without using globals, I'd be very pleased to learn and grateful.

I'm afraid the way I pick out parameters is:

parameterrange=[1 2 3];
parameter=parameterrange(counter):

and then I do my for counter=1:3;

and store results

results(counter)=Z;

Subject: erratic behaviour from 'for' loops, counter not changing

From: dpb

Date: 21 Oct, 2010 20:21:49

Message: 5 of 5

Paddy Carter wrote:
> Thanks Steve ... I have come across the notion globals are bad before.
> The link you send me to suggests they are primarily a problem because
> you can get too many of them and lose control ... that definitely a
> problem in my case I have about 5, and also, regretably, the advice
> "collecting your globals into a struct (i.e., cleaning up your toolbox)
> and passing it on as a parameter of your functions is the most economic
> way to program in MATLAB " is beyond my comprehension. I am using
> functions that somebody else has written.
> however if somebody can explain to me how I can do simple 'for' loops
> and use the counter to pick out parameters and storage addresses,
> without using globals, I'd be very pleased to learn and grateful.
> I'm afraid the way I pick out parameters is:
>
> parameterrange=[1 2 3];
> parameter=parameterrange(counter):
>
> and then I do my for counter=1:3;
>
> and store results
>
> results(counter)=Z;

Well, in isolation there's nothing wrong w/ any of the above code -- the
problem is you've not shown enough to tell what's actually going on
other than if counter is a GLOBAL it can be modified anywhere and
aliasing is another issue...

Are you, by any chance, running this as a script rather than a function?

The key is encapsulation -- make a function that contains the for loop
and pass the parameterRange vector then have the function return the
results vector.


parameterRange=[1,3]; % only need start end, not full vector
...
yourResults = yourComputationalFunction(parameterRange);
...

where

function y = yourComputationalFunction(x, anyotherneededvars)
   y = zeros(1,x(end)-x(1)+1); % preallocate
   for idx = x(1):x(end)
     y(idx) = Z;
   end

or, if you can vectorize Z, then

function y = yourComputationalFunction(x, anyotherneededvars)
   y = Z([x(1):x(end)];

would perhaps be more efficient

Salt to suit...

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us