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:
What's the fix with the loop counter?

Subject: What's the fix with the loop counter?

From: tinne123

Date: 21 May, 2010 12:49:04

Message: 1 of 10

Hi folks,

strange things are happening. Acc to hlp "for x=initval:endval, statements, end repeatedly executes statements .. counter variable x is initialized to value initval and automatically increments by 1 each time through the loop " ..

I used to use it precisely this way and all went fine. Now, what happened .. With the following code, "k" keeps to be 1 while the loop iterates, thus ML just rewrites what it did before .. the resulting apanel is 15x1.
--------------------------------
% suppose there exists an empty 'joint.mat' and each of the single1.mat and single2.mat contain the variable a(15,1) double
% the purpose of the code is to create apanel in 'joint.mat' in that the a-s from the single .mat files are stored under each other
 
N = 2;
for k = 1:N
    matfilename = sprintf('single%d.mat', k);
    load(matfilename)
    load('joint.mat')
    if k == 1
        apanel = a;
    end
    if k > 1
        temp = apanel;
        apanel = [temp; a];
    end
    clear matfilename a temp;
    save ('joint') % INSERT COUNTER UPDATE
end
______________________

However, if I add before the save ('joint') this line:
k = k + 1;
ML does append to apanel one more matrix so that apanel is 30x1. In the end, k = 3.

I am now a bit puzzled by the behaviour of the loop counter? Why did the loop counter update in my other codes as it should, whereas here not? Could it be influenced by the endval? In this case N = 2, in previous codes it was higher.

Many thanks for explanation!!

Subject: What's the fix with the loop counter?

From: Steven Lord

Date: 21 May, 2010 13:12:59

Message: 2 of 10


"tinne123 " <nastanova@yahoo.co.uk> wrote in message
news:ht5vg0$lm$1@fred.mathworks.com...
> Hi folks,
>
> strange things are happening. Acc to hlp "for x=initval:endval,
> statements, end repeatedly executes statements .. counter variable x is
> initialized to value initval and automatically increments by 1 each time
> through the loop " ..
>
> I used to use it precisely this way and all went fine. Now, what happened
> .. With the following code, "k" keeps to be 1 while the loop iterates,
> thus ML just rewrites what it did before .. the resulting apanel is 15x1.

Yes, looking at your code, that's to be expected.

> --------------------------------
> % suppose there exists an empty 'joint.mat' and each of the single1.mat
> and single2.mat contain the variable a(15,1) double
> % the purpose of the code is to create apanel in 'joint.mat' in that the
> a-s from the single .mat files are stored under each other
>
> N = 2;
> for k = 1:N
> matfilename = sprintf('single%d.mat', k);
> load(matfilename)

This loads ALL the variables that were saved in the MAT-file into the
current workspace, thus overwriting any variables that happened to share
names with a variable in that MAT-file.

> load('joint.mat')

The same behavior as above happens here -- but this time, the variable "k"
happens to share a name with a variable from the MAT-file, and so the loaded
value overwrites the loop variable.

One way to avoid this is to LOAD your MAT-file into a struct array:

    mydata = load(matfilename);

and extract ONLY those pieces you need from it:

    apanel = mydata.apanel;

> if k == 1
> apanel = a;
> end
> if k > 1
> temp = apanel;
> apanel = [temp; a];
> end
> clear matfilename a temp;

You don't need to do this; when the loop restarts, matfilename and temp will
be overwritten (because k will be greater than 1) and I'm assuming you're
loading a from one of the MAT-files; when you make the change I suggested
above, you won't have a variable a but will instead have a field a in the
struct array (which will also be overwritten at the next iteration.)

> save ('joint') % INSERT COUNTER
> UPDATE

When you SAVE here, you're SAVEing the value of k and that saved value gets
LOADed up above. Alternately, specify the variables to be saved as
additional input arguments:

save('joint', 'apanel')

*snip*

I advise you not to try to use MAT-files to pass data from one iteration of
the loop into another -- just let the normal flow of execution do so.

--
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: What's the fix with the loop counter?

From: tinne123

Date: 21 May, 2010 14:16:06

Message: 3 of 10

Hi Steve,

thanks for your advise based on which I created
--------------
N = 2;
for ii = 1:N
    S = load(sprintf('single%d.mat', ii),'a');
    load('joint.mat')
    apanel = S.a;
end
save('joint', 'apanel');
---------------
It does not do what I want - namely put together apanel in joint.mat so that it contains a 15x1 from single1.mat and below it a 15x1 from single2.mat.

I have obvious difficulties with programming and I run into troubles with a paper. But I completely agree with you that I should learn how to do it properly. Please Steve (and all users), if possible, may I ask you to have one more look on the story from the beginning?

Input:
single1.mat .. contains a = (1:1:15)';
single2.mat .. contains a = (16:1:30)';
joint.mat .. is empty for now

Output: I need to end up with apanel in joint.mat, so that apanel = [a; a]; with first a coming from single1.mat and the second a from single2.mat. (In reality there are more "single" files, this is just to simplify things).

I am almost sure the thing is easy. But not for me. Please, guys, have mercy, I am in troubles. Many thanks.

Subject: What's the fix with the loop counter?

From: Jos (10584)

Date: 21 May, 2010 14:33:04

Message: 4 of 10

"tinne123 " <nastanova@yahoo.co.uk> wrote in message <ht5vg0$lm$1@fred.mathworks.com>...
< SNIP ... change loop variable inside loop problem

This example might be helpful:

for K=1:5,
   disp(K)
   K = 1 ; % modify K, will the loop ever end?
   disp(K)
end
disp(K)

At the beginning of each iteration, K gets assigned the next value.
My advice: do not modify the looping variable inside the loop!

hth
Jos

Subject: What's the fix with the loop counter?

From: tinne123

Date: 21 May, 2010 14:45:05

Message: 5 of 10

Thanks, Jos,
but as said I am not able to write down a complete code that would do the task I need it to do. I have posted already some drafts, and also obtained a couple of partial tips. But at the moment I 'd really need somebody is that kind and writes down the few lines of a complete code. I still hope that it is an easy thing for one of the users. Thanks everybody, I am waiting for a rescueing post!

Subject: What's the fix with the loop counter?

From: Steven Lord

Date: 21 May, 2010 15:04:44

Message: 6 of 10


"tinne123 " <nastanova@yahoo.co.uk> wrote in message
news:ht64j6$b0q$1@fred.mathworks.com...
> Hi Steve,
>
> thanks for your advise based on which I created
> --------------
> N = 2;
> for ii = 1:N
> S = load(sprintf('single%d.mat', ii),'a');
> load('joint.mat')
> apanel = S.a; end
> save('joint', 'apanel');
> ---------------
> It does not do what I want - namely put together apanel in joint.mat so
> that it contains a 15x1 from single1.mat and below it a 15x1 from
> single2.mat.

Preallocate apanel to be the size you want it to have when it's finished,
then fill it in during the loop.

N = 2;
apanel = zeros(15, N);
for ii = 1:N
     S = load(sprintf('single%d.mat', ii));
    apanel(:, ii) = S.a;
end

Note that this assumes that all the a's are 15-by-1; if not you may need to
use a cell array instead of a numeric matrix or array.

--
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: What's the fix with the loop counter?

From: Sean

Date: 21 May, 2010 15:14:04

Message: 7 of 10

"tinne123 " <nastanova@yahoo.co.uk> wrote in message <ht669h$3nl$1@fred.mathworks.com>...
> Thanks, Jos,
> but as said I am not able to write down a complete code that would do the task I need it to do. I have posted already some drafts, and also obtained a couple of partial tips. But at the moment I 'd really need somebody is that kind and writes down the few lines of a complete code. I still hope that it is an easy thing for one of the users. Thanks everybody, I am waiting for a rescueing post!

N = 2;
apanel = zeros(N*15,1);
idstart = size(apanel,1)-14
for k = 1:N
    matfilename = sprintf('single%d.mat', k);
    load(matfilename); %retrieve a
    apanel(idstart:idstart+14) = a;
    idstart = idstart-14; %NOTE this is not the for-loop variable
end
save('joint.mat','apanel');

Subject: What's the fix with the loop counter?

From: Sean

Date: 21 May, 2010 15:21:04

Message: 8 of 10

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message
> N = 2;
> apanel = zeros(N*15,1);
> idstart = size(apanel,1)-14
> for k = 1:N
> matfilename = sprintf('single%d.mat', k);
> load(matfilename); %retrieve a
> apanel(idstart:idstart+14) = a;
> idstart = idstart-14; %NOTE this is not the for-loop variable
> end
> save('joint.mat','apanel');

Doh! Those should be 15s. Ohwell, Steve Lord's implementation coupled with a (:) or reshape() at the end is better anyway.

Subject: What's the fix with the loop counter?

From: tinne123

Date: 21 May, 2010 15:42:05

Message: 9 of 10

Sean,
thank you, I also tried to think in this way originally, but look, the proposed code results in
apanel =

     0
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15

I think it is better trying to append the matrix after each other.

Subject: What's the fix with the loop counter?

From: tinne123

Date: 21 May, 2010 16:59:05

Message: 10 of 10

A big thank you Steve and Sean,

your posts were helpful. I am sorry for the strange "timing" of my last post, in reality I wrote it before I could see your last posts displayed.

Finally I came up with this:
-----------------------
load('joint.mat')
N = 2;
T = 15;
apanel = zeros(N*T,1);
save ('joint')
clear apanel;
for ii = 1:N
     S = load(sprintf('single%d.mat', ii));
    apanel((1+(ii-1)*T):(ii*T),1) = S.a;
    save ('joint')
end
clear
--------------------------
so now apanel contains the a-s below each other.

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