From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Combine variable length vectors into one vector
Date: Thu, 16 Aug 2012 16:06:08 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 48
Message-ID: <k0j5pg$aob$>
References: <k0c1vj$2h3$> <k0c5do$9t1$> <k0c8lb$o7s$> <k0cbr3$l8l$> <k0ce5i$d4d$> <k0ck1g$31s$> <k0do6p$gp9$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1345133168 11019 (16 Aug 2012 16:06:08 GMT)
NNTP-Posting-Date: Thu, 16 Aug 2012 16:06:08 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 3536503
Xref: comp.soft-sys.matlab:776604

^^^^Ah, thanks.

You were right about the speed. So, what I ended up doing was going with your original suggestion (in the previous post). I just performed the concatenation when I defined the vectors themselves.i.e. at that step the vector was available, so I just used it rather than using filenames. This was much easier than working with cells containing the filenames. Your previous post worked exactly as expected. That was really all I needed. Thanks for this.

I've got another question. This is about loading filenames into a cell array. Specifically, it is about auto pre-allocating a cell.

I've got a cell array that loads a certain set of files (based on the filenames) the first time through a loop. The second time, it loads a different set of files.

EXAMPLE of what is happening:
A file named "List.txt" is provided with the contents being:

Then the loop does the following (loads files with one of these 3 strings in their filenames):
ex. i = 1 --> dataa1bc1.txt, dataa1bc2.txt, dataa1bc3.txt, dataa1bc4.txt
 i = 2 --> datad1ef1.txt, datad1ef2.txt, datad1ef3,.txt datad1ef4.txt
 i = 3 --> datag1hi1.txt, datag1hi2.txt, datad1ef3.txt, datad1ef4.txt

Now, look at line three (i = 3). The problem is that for the third iteration, there are only two files that should be loaded into the cell, not 4. So, the cell should shrink from 4 X 1 (for i = 1 and i = 2) to 2 X 1 for i = 3. Unfortunately, this isn't happening. So for i = 3, the cell holds 2 correct filenames (the first 2) and 2 incorrect filenames (the last 2).

Here is the actual code:


for abcd=1:numel(nnn{1});
    filess = dir(['*' char(nnn{1}(abcd)) '*L*.txt'])
      for kk = 1:numel(filess);

As you can imagine, the next step involves loading the contents of the cell, one row at a time. Well this works well for i = 1 and i = 2 (assuming that numel(nnn{1}) = 3...though this number is not known ahead of time so pre-allocating using cell() is a problem), but for i = 3, 2 wrong filenames are being loaded as I mentioned above. Is there a way to auto shrink the cell array size? Or is there a more effective (alternative) way to load these files?

PS: In the code above, f the line starting with filess, n contains: