#!/usr/bin/env perl # # perl script to convert an input file of variable precision numbers # into Matlab commands that will load the numbers into a # two-dimensional array. The name of the array defaults to vpacvt, # but can be specified as an optional argument on the command line. # The Matlab commands are sent to standard output. They can then be # evaluated in Matlab or saved in an M-file # # Daniel Trinkle # Purdue University # 27 March 2009 my $usage = "Usage: vpaconvert input-file [array-name]\n"; my $inputfile; # input file name my $array = "vpacvt"; # output array name my @input; # input file contents my $digits = 32; # default digits my $row = 0; # current row of numbers (ignoring other input) # parse command line if (($#ARGV < 0) || ($#ARGV > 1)) { print($usage); exit 0; } $inputfile = $ARGV[0]; $array = $ARGV[1] if defined($ARGV[1]); # load input file open(IN, "<$inputfile") || die ("Cannot open input file $inputfile\n"); chomp(@input=); # determine maximum digits of precision necessary for $_ (@input) { /^[0-9\s\.]*[0-9][0-9\s\.]*$/ && do { my $cols = split; for (my $col = 0 ; $col < $cols ; $col++) { my $n = @_[$col]; $n =~ s/^0//; # dont count leading 0 $n =~ s/\.//; # dont count decimal point $digits = length($n) if (length($n) > $digits); } } } print "digits = $digits;\n"; # process input for $_ (@input) { /^[0-9\s\.]*[0-9][0-9\s\.]*$/ && do { $row++; my $cols = split; for (my $col = 0 ; $col < $cols ; $col++) { printf("%s(%d,%d) = vpa(\'%s\',%d);\n", $array, $row, $col+1, @_[$col], $digits); } next; }; printf("%% %s\n", $_); }