Saturday, October 28, 2006

Matlab: Working with text data files using Perl (2)

This post is continuation of the former post concerning Matlab and Perl work with text files.
Former program, reading rectangles.txt file can be of course written in Matlab, without any Perl help. My example with rectangles.txt is quite simple as far as text file is concerned; nevertheless, I used Perl to read and search this file in order to show how Perl can be used within Matlab.

Below I present Matlab code, that does exactly the same task as Perl script presented in my former post.


function [rect_1 rect_2 point]= getRects(filename)

rect_1=[];
rect_2=[];
point=[];

fid=fopen('rectangles.txt');

while true
tline = fgetl(fid);
if tline(1)=='#', continue;end;
if ~ischar(tline), break, end;

[file,theRest] = strtok(tline);
if ~strcmp(file,filename), continue; end;

RECTs= sscanf(theRest,'%f')';

rect_1=RECTs(1:4);
rect_2=RECTs(5:8);
point=RECTs(end-1:end);

end

fclose(fid);

The question now is which method (Matlab+Perl or Matlab only) is faster?

To check it I used tic;getRects('105aty.tiff'); toc; combination in Matlab both for full Matlab script and Matlab/Perl script. Each script was tested 100 times.

The results are:
Matlab only version : 0.18 (std=0.03) sec
Matlab+Perl version: 0.10 (std= 0.02) sec

It is clearly seen that connection Matlab+Perl to work with text data is considerably faster
that of Matlab only.

Appendix:
Test was made on:
Matlab 7.0.0 (R14) and Perl 5.6.1 running on Sun Fire V440 Server.
std - standard deviation