Path: news.mathworks.com!newsfeed-00.mathworks.com!news.tele.dk!feed118.news.tele.dk!7no15843389wig.0!news-out.google.com!q11ni163385259wiw.1!nntp.google.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!217.73.144.44.MISMATCH!feeder.ecngs.de!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!gegeweb.org!aioe.org!.POSTED!not-for-mail
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: Aioe.org NNTP Server
Lines: 152
Message-ID: <k0jevo$f0c$1@speranza.aioe.org>
References: <k0c1vj$2h3$1@newscl01ah.mathworks.com> <k0c5do$9t1$1@speranza.aioe.org> <k0c8lb$o7s$1@newscl01ah.mathworks.com> <k0cbr3$l8l$1@speranza.aioe.org> <k0ce5i$d4d$1@newscl01ah.mathworks.com> <k0ck1g$31s$1@speranza.aioe.org> <k0do6p$gp9$1@speranza.aioe.org> <k0j5pg$aob$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: NPWgRAQosc1uaTm2Hdf8VA.user.speranza.aioe.org
Mime-Version: 1.0
X-Complaints-To: abuse@aioe.org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.28) 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: news.mathworks.com 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
>
> QUESTION:
> 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 =
     'a1bc'
     'd1ef'
     'g1hi'
 >>

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
*a*L*.txt
*1*L*.txt
*b*L*.txt
*c*L*.txt
 >>

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

dir('dataa1bc*.txt')

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
*a1bc*L*.txt
*d1ef*L*.txt
*g1hi*L*.txt
 >>

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']);
row=1;
for k = 1:length(d);
   file=d(k).name;
   M{row,1}=d(row).name;
   row=row+1;
end


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:
     name
     date
     bytes
     isdir
 >> M=cellstr({d.name}')
M =
     '30test.dat'
     'AIRPORT.dat'
     'alldat.dat'
...[elided for brevity]...
     'TESTIT.DAT'
     'VEHIC.DAT'
     'WORK.DAT'
 >>

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

--