help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: reading ascii file into matrix


From: John L Daschbach
Subject: Re: reading ascii file into matrix
Date: Thu, 2 May 96 16:09:27 PDT

>>>>> "Dan" == Daniel A Powers <address@hidden> writes:

    Dan> Hi:

    Dan> This seem like something straigtforward that I can't find in
    Dan> the manual.  I've tried a number of things like fopen, fread,
    Dan> load.  I need something like the GAUSS load command:

    Dan> load dta[2176,21]=c:\dat\sp2b.dat;

    Dan> Any programming examples would be appreciated.

    Dan> Thanks, Dan


Dan,
        I was unable to find this built into octave as well, so I
wrote a little script.  This verison requires you to name the output
file, some I have don't.  
        It would be possible to work this into a matlab wrapper, so
that you could call something like:

foodata = loaddata('foo.dat');

        I find writting octave code which needs to be runtime
evaluated a bit more work than Perl for sure so I use this approach.
Works for me.
        Now if we could add some of the Perl string functions to
octave that would really be a winner.  What I do now is to write a
template file and then use Perl to fill in the template.  This is used
for plotting data analyzed in octave where I want labels and other
values placed in the plot.  You can do it in octave with a lot of
sprintf() and eval() calls, but with such limited string functions
it's easier to do it in Perl from a template.

-John

        P.S. file_exists() is an octave function to check for files.
#
#
#
if ( !file_exists('ti10492.mat') )
  system("$HOME/Perl/xps2matlab.pl -file ti10492.mat ti1049_2.txt");
endif  
load -f ti10492.mat;
#

#!/msrc/apps/perl-5.0/bin/perl
#
#  xps2matlab.pl
#
#  Purpose:  Convert XPS data into Matlab (or Octave) data
#
#  $Log$
#
require 'newgetopt.pl';
#
#  Global vars
#
$fname = "";                    # if empty use STDOUT
#
#  Handle the options
#
&init_options;
&NGetOpt(@all_opts);
&handle_options;
#
#
$row = 0;
$maxcol = 0;
while (<>) {
  next if ( /^\s*\#/ );
  @dataline = split;
  for $col (0..$#dataline) {
    $data{"$row:$col"} = $dataline[$col];
  }
  ++$row;
  $maxcol = ($#dataline > $maxcol) ? $#dataline : $maxcol;
  $maxrow = $row;
}
#
#  Adjust the number of rows and columns for Octave
#
--$maxrow;
$maxcol;
#
#
print STDERR "Checking <$fname>\n";
if ( $fname ne '' ) {
  $matname = $fname;
  $matname =~ s/\.mat$//;
  print STDERR "MATNAME = <$matname>\n";
  open (DATA,">$fname") || die "Can't open <$fname> for output\n";
  select(DATA);
} else {
  print STDERR "Data written to STDOUT with matrix <indata>\n";
  $matname = "indata";
  select(STDOUT);
}
$columns = $maxcol + 1;
print STDERR "Size is $maxrow\n";
print "# name: $matname\n";
print "# type: matrix\n";
print "# rows: $maxrow\n";
print "# columns: $columns\n";
#
#
for $row (0..$maxrow) {
  for $col (0..$maxcol) {
    printf("% 8.2f ",$data{"$row:$col"});
  }
  printf("\n");
}
exit;
#
#
sub init_options {
  @gen_opts = ('h','help','v','verbose');
  @all_opts = ('f:s', 'file:s');
  push(@all_opts,@gen_opts);
}
#
#
#
sub handle_options {
  &help && exit if ( $opt_h || $opt_help );
  $verbose = $opt_v || $opt_verbose ? 1 : 0;
  $fname = $opt_f if ( $opt_f );
  $fname = $opt_file if ( $opt_file );
  print STDERR "FNAME = <$fname>\n";
}
#
#
#
sub help {
  print "Program:    xps2matlab.pl   A Perl script\n";
  print "Purpose:    \n";
  print "Usage:      xps2matlab.pl infile [options] > outfile\n";
  print "Options:    -h        this help\n";
  print "            -v        verbose\n";
  print "            -verbose  verbose\n";
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]