2012-01-05 110 views
3

我有一个调用Perl脚本的matlab函数,该函数将大型文本文件转换为二进制文件以便在Matlab中使用。看到这里的Perl脚本的详细信息:Parsing unsorted data from large fixed width text在Matlab中实时显示Perl脚本的输出

我的Matlab的功能看起来像这样

function convertMyData(dataFileName) 

%Do some checks on the data 
disp('Done Checking Stuff!'); 

%Process data file with Perl 
perl('myPerlScript.pl',dataFileName) 

% More Processing on the Binary output from Perl 
disp('All Done!'); 

在perl脚本是表示,因为它可能需要几分钟到转换脚本的进度一些打印语句。事情是这样的:

while ($line = <INFILE>) { 
    if ($lineCount % 100000 == 0){ #Display Progress every 100,000 lines 
     print "On Line: ".$lineCount."\n"; 
    } 
    #PROCESS LINE DATA HERE 
    $lineCount ++; 
} # END WHILE <INFILE> 
print "Finished Reading: ".$lineCount." Lines\n"; 

的问题是,在Matlab我所有的“上线:XXXXX”一旦脚本完成,而不是在像Matlab的disp()提示实际显示print声明只是被人抛弃到Matlab的默认ans变量功能。

那么如何在外部程序的输出显示在Matlab提示符运行时(如果可能)呢?

回答

2

我不认为你可以做到这一点。 MATLAB将控制权传递给perl解释器,然后返回结果。

有一种解决方法适用于我。首先在您的perl脚本中添加local $|=1;以打开STDOUT autoflush。在任何输出到STDOUT之前。 (参见,例如,here对清除缓冲区的更多细节。)然后调用使用system功能的Perl:如果你的Perl解释器位于带有空格的路径

system(['"path_to_perl\perl.exe" test.pl ' dataFileName]); 

双引号是非常重要的。

1

尝试使用内置的perl命令。它将运行perl解释器并返回结果。我认为你需要把你的输出放在一个名为result的变量中。

从文档:

结果=的perl(...)返回试图Perl的结果调用的结果。

0

我有一个类似的问题,并且goole引导我到你的问题。

最后,在Windows上,我使用下面的matlab代码,所以解决我的问题。

cmdString = 'start /WAIT '; 
cmdString = [cmdString 'C:\Strawberry\perl\bin\perl extract_tti_trace.pl "' fullname '"']; 
dos(cmdString)