From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Combine variable length vectors into one vector
Date: Thu, 16 Aug 2012 13:43:01 -0500
Organization: NNTP Server
Lines: 152
Message-ID: <k0jevo$f0c$>
References: <k0c1vj$2h3$> <k0c5do$9t1$> <k0c8lb$o7s$> <k0cbr3$l8l$> <k0ce5i$d4d$> <k0ck1g$31s$> <k0do6p$gp9$> <k0j5pg$aob$>
Mime-Version: 1.0
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20120306 Thunderbird/3.1.20
X-Notice: Filtered by postfilter v. 0.8.2
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Xref: comp.soft-sys.matlab:776610

On 8/16/2012 11:06 AM, Stan wrote:
> ^^^^Ah, thanks.
> You were right about the speed. ...

Toldja!  <vbg>  :)

> ... 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.

If your algorithm is such that you know or compute the length of these 
vectors, you can minimize the concatenation time growth by allocating 
the longer vector and then populating it appropriately.  Or, at least 
minimize the number of reallocations by starting w/ some large length M 
and don't grow the size until you've used that amount up and need more; 
then grow it by another sizable chunk.  The extra overhead in the 
bookkeeping is worth the time if the process is repetitive.


> A file named "List.txt" is provided with the contents being:
> a1bc
> d1ef
> g1hi
> 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
> Here is the actual code:
> fid_1=fopen('List.txt');
> nnn=textscan(fid_1,'%s');
> fclose(fid_1);
> for abcd=1:numel(nnn{1});
>   filess = dir(['*' char(nnn{1}(abcd)) '*L*.txt'])
>   spec_fc_row=1;
>   for kk = 1:numel(filess);
>     file=filess(kk).name;
>     M{spec_fc_row,1}=filess(spec_fc_row).name;
>     spec_fc_row=spec_fc_row+1;
>   end
> end
> 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:
> 'a1bc'
> 'd1ef'
> 'g1hi'

Well, you kinda' lost me but let me try and see where I go wrong...

I start w/ a cell array c w/ your file partial names to match...

 >> c
c =

Since I don't have a bunch of files matching these, dir() here will be 
null but I _can_ look at the argument to the dir() function --

 >> for i=1:numel(c{1}),disp(['*' char(c{1}(i)) '*L*.txt']),end

That doesn't look at all like your earlier description of wanting


etc., for the strings in the file...also note there are four elements 
there not just the three strings.  I think here's where most of your 
problems arise.

To do what you described above you don't want the loop over numel(c{1}) 
w/ the curly brackets on the first element of the cell array--the 
curlies dereference the _content_ of the cell which is a 4-character 
character array.  You want to loop instead on the number of cell strings 
in c.

Try this instead--
 >> for i=1:length(c),disp(['*' char(c(i)) '*L*.txt']),end

In your code as the argument to dir() this will result in returning the 
files you wish in the proper number.

Now let me see if I can decipher intent of inner loop...that confused me 
on first reading--

d = dir(['*' char(c(i)) '*L*.txt']);
for k = 1:length(d);

OK, in the end you've got a cell array M that contains the names of the 
directory structure.  Why?  Why not just use the name itself to do 
whatever you want with opening the file(s)?

I don't know where you're headed so I don't really see the next step 
clearly, sorry.

If you really do just want a cell array of the names for some reason, 
you can accomplish what the above loop does simply by (I demonstrate 
here on a local directory result)

 >> d=dir('*.dat')
d =
16x1 struct array with fields:
 >> M=cellstr({}')
M =
...[elided for brevity]...

See if that helps and if still can't get where going, try again. 
Probably ought to start a new thread for that question; this is pretty 
far afield from where this one started...