Microsoft Word to Matlab Matrix Conversion

I type in tons of equations in papers in MS Word (yeah, I need to learn LaTex). So when I have matrices in MS Word, you can't paste into Matlab because the MS Word format is funky. The perlie below will take care of it. Now I need to figure out a way to have it monitor the clipboard and automatically convert it if it detects a MSW matrix:


#echo Part is Microsoft Word Equation:
echo "■(1&0&0@1&-4&2@1&2&-4)"|perl -lne 's/\&/ /g;s/\@/;/g;s/\(/\[/g;s/\)/\]/g;print "A = $1" if m/(\[.+\])/g'

# Matlab Format:
A = [1 0 0;1 -4 2;1 2 -4]
And I decided to make it bidirectional (goes from Matlab to MS Word and vice versa). Pretty awesome...saving me tons of time:
#!/usr/bin/perl
use strict;
use warnings;
print "\n";
my $bit = 0;
my @global;
while(<>){
 if(m/\@|\&/){
  &to_matlab($_);
 }else{
  if(m/ans =/g){
   $bit = 1;
  }
  if( $bit == 1 && $_ =~ m/\s+((-|\d*)\d+\.*\d*)/g){
   push(@global,$1);
  }else{
   &to_word($_);
  }
 }
}

sub to_word {
 my $in = shift;
 $in =~ s/\;/\@/g;
 $in =~ s/\s/\&/g;
 $in =~ s/\[/\(/g;
 $in =~ s/\]/\)/g;
 print "[■$1]\n" if( $in =~ m/(\(.+\))/g );
}
sub to_matlab {
 my $il = shift;
 my @in = split(/■/,$il);
 my @A = ('A' .. 'Z');
 foreach(@in){
  $_ =~ s/\]//g;
  $_ =~ s/\&/ /g;
  $_ =~ s/\@/;/g;
  $_ =~ s/\(/\[/g;
  $_ =~ s/\)/\]/g;
  my $name = "$A[ rand(25) ]$A[ rand(25) ]";
  print "$name = $1\n" if m/(\[.+\])/g;
 }
}
my $ans = join(';',@global);
$ans = "[$ans]";
&to_word($ans);


Comments

Popular posts from this blog

Using Audacity as an Oscilloscope

Using Google Command Line Tools to post my Computer Systems Notes

Ping utility that targets a specific port