在Perl程序中可嵌入matlab程序块吗?我想用matlab程序画条形图,整体采用perl语言编写程序。
追问:谢谢!我想用程序实现画条形图,因为数据比较多,有几千组数据,每组中有10多条多肽,我要统计每组中每种氨基酸的百分比,所以有几千个条形图。希望你给我些建议!
追问:谢谢!我想用程序实现画条形图,因为数据比较多,有几千组数据,每组中有10多条多肽,我要统计每组中每种氨基酸的百分比,所以有几千个条形图。希望你给我些建议!
答案:可以看下这个程序利用OLE调用matlab opc通信,应该是比较简单的
http://read.pudn.com/downloads188/sourcecode/math/883126/callmatlab.pl__.htm
use Win32::OLE; # Win32::OLE 方法: Invoke LastError QueryObjectType
use Win32::OLE::Variant;
# Simple perl script to execute commands in Matlab.
# Note the name Win32::OLE is misleading and this actually uses COM!
#
# $Revision: 1.2 $ $Author: batserve $ $Date: 2004/12/01 14:15:22 $
# Use existing instance if Matlab is already running.
eval {$ml = Win32::OLE->GetActiveObject('Matlab.Application')}; # eval是异常处理机制
die "Matlab not installed" if $@;
unless (defined $ml) {
$ml = Win32::OLE->new('Matlab.Application') # 创建matlab对象 excel中用$xl = Win32::OLE->new(“Excel.Application“);
or die "Oops, cannot start Matlab";
}
# Examples of executing MATLAB commands - these functions execute in
# MATLAB and return the status.
# 定义matlab中的命令
@exe_commands = ("IRK = pdbread('pdb1irk.ent');",
"LCK = pdbread('pdb3lck.ent');",
"seqdisp(IRK);",
"seqdisp(LCK);",
"[Score, Alignment] = swalign(IRK, LCK,'showscore',1);");
# send the commands to Matlab # 调用前面定义的各matlab命令
foreach $exe_command (@exe_commands)
{ $status = &send_to_matlab('Execute', $exe_command);
print "Matlab status = ", $status, "\n";
}
# 将命令传给matlab的子函数
sub send_to_matlab
{ my ($call, @command) = @_; # 获取传进来的两个参数,$call是'Execute',@command 是$exe_command
my $status = 0;
print "\n>> $call( @command )\n"; # 输出两个参数
$result = $ml->Invoke($call, @command); # 调用matlab运行命令, $result是matlab的返回结果
if (defined($result))
{ unless ($result =~ s/^.\?{3}/Error:/)
{ print "$result\n" unless ($result eq "");
}
else
{ print "$result\n";
$status = -1;
}
}
return $status;
}
# Examples of passing variables between MATLAB and Perl. 两者之间参数的传递
#
# MATLAB supoprts passing character arrays directly with the following syntax:
#
# PutCharArray([in] BSTR name, [in] BSTR workspace, [in] BSTR string);
# GetCharArray([in] BSTR name, [in] BSTR workspace, [out] BSTR string);
&send_to_matlab('PutCharArray', 'centralDogma', 'base', 'DNA->RNA->Protein.');
&send_to_matlab('GetCharArray', 'centralDogma', 'base');
# Numeric arrays can be passed by reference in a SAFEARRAY using the
# PutFullMatrix and GetFullMatrix functions.
#
# PutFullMatrix([in] BSTR name, [in] BSTR workspace, [in] BSTR data);
# GetFullMatrix([in] BSTR varname, [in] BSTR workspace, [out] BSTR retdata);
$mReal = Variant(VT_ARRAY|VT_R8, 4, 4); # Win32::OLE::Variant 变量的定义格式,在DOS下用perldoc -i Win32::OLE::Variant了解其详细信息
$mImag = Variant(VT_ARRAY|VT_R8, 4, 4); # VT_ARRAY--矩阵,VT_DATE--日期,VT_BSTR--字符串
$mReal->Put([[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0]]); # 给Variant类型变量赋值
print "\n>> PutFullMatrix( 'magicArray', 'base', ",'$mReal, $mImag'," )\n";
$ml->PutFullMatrix('magicArray', 'base', $mReal, $mImag); # 数据传入matlab,以magicArray为变量名的矩阵
$ml->Execute('magicArray = magic(4)'); # 执行matlab命令,定义矩阵magicArray并赋值
$m2Real = Variant(VT_ARRAY|VT_R8|VT_BYREF,4,4);
$m2Imag = Variant(VT_ARRAY|VT_R8|VT_BYREF,4,4);
print "\n>> GetFullMatrix( 'magicArray', 'base', ",'$m2Real, $m2Imag',
" )\n";
$ml->GetFullMatrix('magicArray', 'base', $m2Real, $m2Imag); #从matlab中获取矩阵
for ($i = 0; $i 4; $i++) {
printf "%3d %3d %3d %3d\n", $m2Real->Get($i,0), $m2Real->Get($i,1)
,
$m2Real->Get($i,2), $m2Real->Get($i,3)
;
} # $m2Real->Get($i,0) 是第$i行第1列元素,$m2Real->Get($i,3)是第$i行第4列元素
# Additionally, you can use Variants to send scalar variables by reference
# to MATLAB for all data types except sparse arrays and function handles through
# PutWorkspaceData:
# PutWorkspaceData([in] BSTR name, [in] BSTR workspace, [in] BSTR data);
#
# Results are passed back to Perl directly with GetVariable:
# HRESULT = GetVariable([in] BSTR Name, [in] BSTR Workspace);
# Create and initialize a date Variant.
$dnaDate = Variant->new(VT_DATE|VT_BYREF, 'Feb 28, 1953');
&send_to_matlab('PutWorkspaceData', 'dnaDate', 'base', $dnaDate);
&send_to_matlab('Execute', 'dnaDate');
# Create and initialize a new string Variant.
$aminoString = Variant->new(VT_BSTR|VT_BYREF, 'matlab');
$aa = Variant->new(VT_BSTR|VT_BYREF, ' matlab');
&send_to_matlab('PutWorkspaceData', 'aminoAcids', 'base', $aminoString);
&send_to_matlab('PutWorkspaceData', 'a_value', 'base', $aa); # 将perl中的变量值付给matlab中的变量
# Change the value in MATLAB
&send_to_matlab('Execute', "aminoAcids = 'ARNDCQEGHILKMFPSTWYV';"); # 执行matlab中的命令
# Bring the new value back
$aa = $ml->GetVariable('aminoAcids','base');
$aa = $ml->Invoke('GetVariable', 'aminoAcids', 'base'); #'GetCharArray'是命令名, 'aminoAcids'是matlab中的变量,
$aa = $ml->Invoke('GetCharArray', 'aminoAcids', 'base'); #'GetCharArray'是命令名, 'aminoAcids'是matlab中的变量,
printf "Amino acid codes: %s\n", $aa;
undef $ml; # close Matlab if we opened it
另外可以参考
http://blog.csdn.net/zhuliting/article/details/6009052
这个需要自己编译xs文件会麻烦一点
其他:画条形图不一定要用matlab,可以直接使用excel啊
上一个:lingo中怎么调用matlab程序?
下一个:请问一下,在Matlab软件中龙格库塔法,为什么按照课本编的程序都不能运行呢?求高手。!!!