[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Octave beginner-reading info from text file
From: |
Jordi Gutiérrez Hermoso |
Subject: |
Re: Octave beginner-reading info from text file |
Date: |
Wed, 15 Dec 2010 09:23:11 -0600 |
On 15 December 2010 05:58, Zamuel <address@hidden> wrote:
> I guess I took the hard way and managed to separate the Perl data
> dumber format to a string (ex. class_b='1','2').
Uhm, why? Just abandon this path and use "load". It's never too late
to abandon a bad idea.
Or use Perl to parse Perl. Octave has the perl command for this
purpose. The Perl script to turn Data::Dumper back into Perl variables
is at the end of this email. Run this Perl script, and then use
Octave's load function to turn it into an Octave matrix.
HTH,
- Jordi G. H.
-----------------------------------------------------------------------
#!/usr/bin/perl -w
#
# Use perl("read.pl filename") from Octave to read the Perl matrix from
# filename into Perl.
my $dumper_string;
{
local $/=undef;
open FILE, $ARGV[0] or die "Couldn't open file: $!\n";
$dumper_string = <FILE>;
}
close FILE;
my $matrix = eval $dumper_string;
die "eval: address@hidden" if $@;
# Now $matrix contains the matrix data, use whatever Perl code to
# output it, e.g.
my $octmatrix = $$matrix{name};
$octmatrix =~ m,/(\w*)/$,; $octmatrix = $1;
open OUTFILE, ">$octmatrix" or die "$!: $octmatrix";
for(my $i = 0; $i < scalar @{$$matrix{data}};$i++){
for(my $j = 0; $j < scalar @{$$matrix{data}[$i]}; $j++){
print OUTFILE $$matrix{data}[$i][$j]." ";
}
print OUTFILE "\n";
}
# Now you should have an ordinary text file, use Octave's "load" to
# read the matrix in.