Prev: Please help me to generate a markov chain.
Next: Can't import Netcdf variable with unlimited dimension correctly
From: tinne123 on 21 May 2010 08:49 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!!
From: Steven Lord on 21 May 2010 09:12 "tinne123 " <nastanova(a)yahoo.co.uk> wrote in message news:ht5vg0$lm$1(a)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(a)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
From: tinne123 on 21 May 2010 10:16 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.
From: Jos (10584) on 21 May 2010 10:33 "tinne123 " <nastanova(a)yahoo.co.uk> wrote in message <ht5vg0$lm$1(a)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
From: tinne123 on 21 May 2010 10:45
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! |