automation and for loops

Jonas Amhest xenonfirexxx at yahoo.com
Wed Jul 1 13:17:40 CDT 2009


Hello all,

First off, thank you so much for your help!

I tried several variations of this but keep getting an empty cell array. It might have to do with the fact that my .txt files are located in a directory. I need to be able to type in the directory name and have it read through each file and save each file in a cell array, new file, or variable without the headers and footers. I've tried running the code below while located in the directory itself and right above the directory as well. 

I'm playing with sticking the 'function data = read_data_file(files)'
function into a loop along with 'files=dir('*.txt');' but am not having any luck...

Thanks!

-Mike

--- On Wed, 6/24/09, James Sherman Jr. <shermanj at umd.edu> wrote:

> From: James Sherman Jr. <shermanj at umd.edu>
> Subject: Re: automation and for loops
> To: "Jonas Amhest" <xenonfirexxx at yahoo.com>
> Cc: "octave" <help-octave at octave.org>
> Date: Wednesday, June 24, 2009, 10:24 PM
> Instead of modifying the code that
> works, I'd break it off into a separate function that
> accepts a filename and returns the data variable. 
> Something like:
> 
> in file called read_data_file.m:
> 
> function data = read_data_file(filename)
> 
> data = [];
> 
> fid = fopen(filename);
> 
> <rest of your function>
> return;
> 
> then you can just call this function in your for loop:
> 
> files=dir('*.txt');
> 
> for i=1:length(files)
> 
>         all_data{i} = read_data_file(files(i).name);
> 
> end
> 
> where all_data is called a cell array.  Basically, you can
> treat it like a normal array when accessing elements just
> use {} instead of ().
> 
> Hope this helps.
> 
> On Wed, Jun 24, 2009 at 6:07 PM,
> Jonas Amhest <xenonfirexxx at yahoo.com>
> wrote:
> 
> 
> 
> hi all,
> 
> 
> 
> i'm using octave 3.2 on windows xp.
> 
> i have a lot of files i need to load and save, but it has
> to be automated.
> 
> the files have headers, data, and footers. i know how to
> open these but i want to incorporate a loop where the file
> is loaded and saved to a separate file or even just have the
> data saved to a separate variable through each loop.
> 
> 
> 
> 
> here're are two example files:
> 
> 
> 
> new00.txt
> 
> 
> 
> asdf
> 
> asdf
> 
> 1385.00 5585
> 
> 2446.34 95456.74
> 
> 3234.35 2343456.04
> 
> 4564    53456345
> 
> 5797    83456
> 
> asdf
> 
> 
> 
> 
> 
> and
> 
> 
> 
> new01.txt
> 
> 
> 
> 
> 
> asdf
> 
> asdf
> 
> 1385.00 5585
> 
> 2446.34 95456.74
> 
> 3234.35 2343456.04
> 
> 4564    53456345
> 
> 5797    83456
> 
> asdf
> 
> 
> 
> 
> 
> to open the files i'm using this code so that it skips
> the headers and footers and makes a matrix out of my data:
> 
> 
> 
> data = [];
> 
> fid = fopen('ref00.txt');
> 
> line_string = fgetl(fid);   # reads one line of the file
> into a string
> 
> while (line_string != -1)   # makes sure its not eof
> 
>   [line_data, count] = sscanf(line_string, '%f
> %f');  # tries to parse string; f=decimal notation
> 
>   if (count == 2)   # if its successful (finds 2
> integers)
> 
>     data = [data;line_data'];   # append it to your
> data variable
> 
>   end
> 
>   line_string = fgetl(fid);
> 
> end
> 
> fclose(fid);
> 
> 
> 
> 
> 
> my matrix is saved into the variable data. i was trying to
> put the code above into a for loop that saves new00.txt into
> 'data00' and new01.txt into 'data01'
> 
> based off of this code:
> 
> 
> 
> files=dir('*.txt');
> 
> for i=1:length(files)
> 
>         eval(['load ' files(i).name '
> -ascii']);
> 
> end
> 
> 
> 
> 
> 
> 
> 
> my attempt at this was:
> 
> 
> 
> for i=1:length(files)
> 
> fid = fopen(files);
> 
> line_string = fgetl(fid);   # reads one line of the file
> into a string
> 
> while (line_string != -1)   # makes sure its not eof
> 
>   [line_data, count] = sscanf(line_string, '%f
> %f');  # tries to parse string; f=decimal notation
> 
>   if (count == 2)   # if its successful (finds 2
> integers)
> 
>     data(i) = [data;line_data'];   # append it to
> your data variable
> 
>   end
> 
>   line_string = fgetl(fid);
> 
> end
> 
> fclose(fid);
> 
> end
> 
> 
> 
> but i keep getting an error about double_value () : :wrong
> type argument 'struct'
> 
> 
> 
> 
> 
> 
> 
> any help is greatly appreciated!
> 
> 
> 
> -Mike
> 
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> 
> Help-octave mailing list
> 
> Help-octave at octave.org
> 
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
> 
> 
> 
> 


      




More information about the Help-octave mailing list