3

I have matlab function that calls a Perl script which converts a large text file to binary for use in Matlab. See here for Perl script details: Parsing unsorted data from large fixed width text

My Matlab function looks something like this

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!');

In the perl script are some print statements showing the progress of the script since it can take several minutes to convert. Something like this:

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";

The problem is that in Matlab all of my "On Line: XXXXX" print statements just get dumped to Matlab's default ans variable once the script completes instead of actually displayed at the prompt like Matlab's disp() function.

So how (if possible) do you get an external program's output to show up at the Matlab prompt while it is running?

Community
  • 1
  • 1
Aero Engy
  • 3,588
  • 1
  • 16
  • 27

3 Answers3

2

I don't think you can do it. MATLAB passes the control to perl interpreter and then just get back the results.

There is one workaround that worked for me. First add local $|=1; inside your perl script to turn on STDOUT autoflush. Before any output to STDOUT. (See, for example, here for more details on flushing buffer.) Then call perl using system function:

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

Double-quotes are important if your perl interpreter is located in a path with spaces.

yuk
  • 19,098
  • 13
  • 68
  • 99
1

Try using the built-in perl command. It will run perl interpreter and return result. I think that you need to put your output in a variable named result.

From the documentation:

result = perl(...) returns the results of attempted Perl call to result.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
0

I have a similar problem, and goole lead me to your question.

Finally, on windows, I use the following matlab code, so solve my problem.

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