automation and for loops

James Sherman Jr. shermanj at umd.edu
Wed Jun 24 17:24:14 CDT 2009


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://www-old.cae.wisc.edu/pipermail/help-octave/attachments/20090624/8775637d/attachment.html 


More information about the Help-octave mailing list