Variable Names

Ben Abbott bpabbott at mac.com
Tue Jun 30 18:44:56 CDT 2009


On Jun 30, 2009, at 5:21 PM, Judd Storrs wrote:
>
> On Tue, Jun 30, 2009 at 4:37 PM, josgau33 <skankinkid33 at gmail.com>  
> wrote:
>
>> Hello all,
>>
>> I'm trying to write a program I can use to load a large number of  
>> data files
>> into Octave. The files all have the same naming convention:
>>
>> Res2006001
>>   ...
>> Res2006365
>>
>> Does anyone know if this can be done with an M-file? If so, could  
>> you give
>> me some pointers. Thanks,
>>
>> Joseph
>
> Without knowing the details of the file types or the numbering  
> scheme it's hard to say. If the filenames are sequentially numbered  
> you could do something like this.
>
> First, if the files aren't some standard file handled by octave or  
> the io or misc packages, the first step I recommend writing a  
> function that takes a file name and returns the data from that one  
> file. Let's call that load_my_data(). Then
>
> data = load_my_data("Res2006001") ;
>
> would read the file into data.
>
> Now, to load many of them one approach is to generate filenames  
> using sprintf and store the data in a cell array. Suppose you have  
> 500 files named like "Res2006001" to "Res2006501". Something like  
> this should get you started:
>
> for i=1:500
>     name = sprintf("Res2006%03d", i) ;
>     data{i} = load_my_data(name) ;
> endfor
>
> One thing to be careful of is to make sure that you store data into  
> the cell starting at 1.
>
> If it's difficult to generate the filenames, another option is to  
> store the filenames into a cell array and the load the data using  
> names from the cell array:
>
> filenames = {"Res2006001","Res2006002", ................. ,  
> "Res2006501"};
> for i=1:length(filenames)
>     data{i} = load_my_data(filenames{i}) ;
> endfor

To get the filenames, you can try ...

filenames = {dir("Res2006*").name};

Ben



More information about the Help-octave mailing list