matlab.pd

filter: Statementstmt 81.3% 153 / 188 Branchbran 55.4% 51 / 92 Conditioncond 45.8% 11 / 24 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 80.9% 34 / 42 Podpod 38.2% 13 / 34 totaltotal 66.8% 264 / 395 CC 59 SCAR 21.9
CC
59
Cov
71%
new
27.3
print_all_var_info
20.0
write
12.2
CRAP 9.0
↑ Summary extra_matio.c »
1 ##-*- Mode: Perl -*-
2 use strict;
3 use warnings;
4  
5 our $VERSION = '0.007';
6 pp_setversion("'$VERSION'");
7  
8 my $Matio_src = 'matio-1.5.30/src';
9  
10 # This works nicely, but I am not using it for anything
11 my ($MAT_FT_MAT73,$MAT_FT_MAT5,$MAT_FT_MAT4) = (0,1,2);
12 my %Enums;
13 sub make_enum_hash {
14 open my $fh, '<', "$Matio_src/matio.h" or die "Can't open matio.h";
15 my $in_enum = 0;
16 my $enum_name;
17 while(<$fh>) {
18 if ($in_enum) {
19 if ( /\}/ ) {
20 $in_enum = 0;
21 next;
22 }
23 next unless /^\s+(\w+)\s*=\s*([x\d]+)/;
24 my ($name,$val) = ($1,$2);
25 $Enums{$enum_name}->{$name} = $val =~/x/ ? hex($val) : $val;
26 }
27 if ( /^enum\s+(\w+)/ ) {
28 $enum_name = $1;
29 $in_enum = 1;
30 $Enums{$enum_name} = {};
31 next;
32 }
33 }
34 close $fh;
35 }
36  
37 make_enum_hash();
38  
39 pp_addpm({At=>'Top'}, <<'EO_TOPMATTER');
40  
41 =head1 NAME
42  
43 PDL::IO::Matlab -- Read and write Matlab format data files.
44  
45 =head1 DESCRIPTION
46  
47 This module provides routines to read and write pdls to and from
48 data files in Matlab formats. The module uses the matio C library.
49 Both functional and OO interface are provided.
50  
51 Only real, multi-dimensional arrays corresponding to PDL data types are supported.
52 Compression for both reading and writing is supported.
53  
54 See the section L</CAVEATS> for important information on potential problems when using
55 this module.
56  
57 =head1 SYNOPSIS
58  
59 use PDL;
60 use PDL::IO::Matlab qw( matlab_read matlab_write matlab_print_info);
61  
62 # write two pdls in matlab 5 format
63 matlab_write('file.dat', $x, $y);
64  
65 # read an array of ndarrays
66 # from file in matlab 4, 5, or 7.3 format.
67 my @pdls = matlab_read('file.dat');
68  
69 # write pdl in matlab 7.3 format.
70 matlab_write('file.dat', 'MAT73', $x);
71  
72 matlab_print_info('file.dat');
73  
74 =cut
75  
76 $PDL::onlinedoc->scan(__FILE__) if $PDL::onlinedoc;
77 1 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 use warnings;
80 1 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use PDL::LiteF;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use PDL::NiceSlice;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use PDL::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 use Data::Dumper;
85  
86 EO_TOPMATTER
87  
88 pp_addpm({At => 'Middle'}, <<'EO_MIDDLE');
89  
90 my %Format_list = (
91 MAT73 => 0,
92 MAT5 => 1,
93 MAT4 => 2
94 );
95  
96 my %Inv_format_list = (
97 0 => 'MAT73',
98 1 => 'MAT5',
99 2 => 'MAT4'
100 );
101  
102 # non-OO functions
103  
104 =head1 FUNCTIONS
105  
106 The functional interface.
107  
108 =head2 B<matlab_read>
109  
110 =head3 Usage
111  
112 Return all arrays in C<$filename>
113  
114 @pdls = matlab_read($filename);
115 @pdls = matlab_read($filename, {OPTIONS});
116  
117 Return first array in C<$filename>
118  
119 $x = matlab_read($filename);
120  
121 Do not automatically convert C<1xn> and C<nx1> arrays
122 to 1-d arrays.
123  
124 @pdls = matlab_read($filename, { onedr => 0 } );
125  
126 Reads all data in the file C<$filename>.
127 Formats 4, 5, and 7.3 are supported. Options
128 are passed to L</B<new>>.
129  
130 =cut
131 1 ▶ 
Subroutine matlab_read: called CC=3 SCAR=11.0
Pod documented
132 1 ▶sub matlab_read {
Condition50%1/2
$opts || {}
l!l
01
MC/DC0%0/1
$opts || {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
133 1 my ($filename,$opts) = @_;
134 1 my $mat = PDL::IO::Matlab->new($filename, '<', $opts || {});
135 1 ▶ my @res = $mat->read_all;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
136 $mat->close;
137 wantarray ? @res : $res[0];
138 }
139  
140  
141 =head2 B<matlab_write>
142  
143 =head3 Usage
144  
145 matlab_write($filename,$x1,$x2,...);
146 matlab_write($filename,$format,$x1,$x2,...);
147  
148 Automatically convert C<n> element, 1-d ndarrays to C<1xn> matlab
149 variables.
150  
151 matlab_write($filename,$x1,$x2,..., {onedw => 1} );
152  
153 Automatically convert to C<nx1> matlab
154 variables.
155  
156 matlab_write($filename,$x1,$x2,..., {onedw => 2} );
157  
158 Use zlib compression
159  
160 matlab_write($filename,$x1,$x2,..., {compress => 1} );
161  
162 This method writes pdls C<$x1>, C<$x2>,.... If present, C<$format>
163 must be either C<'MAT5'> or C<'MAT73'>.
164  
165 1 ▶=cut
Subroutine matlab_write: called CC=9 SCAR=23.0
Pod documented
166  
167 1 sub matlab_write {
168 1 ▶ my @strings;
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 1 4
169 3 my @hashes;
170 3 ▶ my @refs;
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 2 1 3
171 2 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'HASH') { } 0 2 2
172 0 my $v = shift;
173 if ( ref($v) ) {
174 if ( ref($v) eq 'HASH' ) {
175 2 push @hashes, $v;
176 }
177 else {
178 push @refs, $v;
179 1 }
180 }
181 else {
182 1 ▶ push @strings, $v;
Branch: 1 branch
BranchTrueFalseTotal
if @strings < 1 or @strings > 2 0 1 1
Condition33%1/3
@strings < 1 or @strings > 2
l!l&&r!l&&!r
001
MC/DC0%0/2
@strings < 1 or @strings > 2
@strings < 1 @strings > 2
Truth table33%1/3
A or B
A= @strings < 1B= @strings > 2
ABresult
1X1
011
000
183 }
184 }
185 1 barf 'matlab_write: ' . scalar(@strings) .
186 1 ▶ ' string arguments given. One or two expected.'
Condition50%1/2
$strings[1] || 'MAT5'
l!l
01
MC/DC0%0/1
$strings[1] || 'MAT5'
$strings[1]
Truth table50%1/2
A
A= $strings[1]
Aresult
11
00
187 1 if @strings < 1 or @strings > 2 ;
188 1 my $filename = $strings[0];
189 my $format = $strings[1] || 'MAT5';
190 my $opth = { format => $format };
191 foreach (keys %{$hashes[0]}) { $opth->{$_} = $hashes[0]->{$_} }
192 1 my $mat = PDL::IO::Matlab->new($filename, '>', $opth);
193 1 ▶ $mat->write(@refs) if @refs;
Branch: 1 branch
BranchTrueFalseTotal
if @refs 1 0 1
194 1 $mat->close;
195 1 scalar(@refs);
196 }
197  
198 =head2 B<matlab_print_info>
199  
200 =head3 Usage
201  
202 # print names and dimensions of variables.
203 matlab_print_info($filename);
204 # also print a small amount of the data.
205 matlab_print_info($filename, { data => 1 });
206 # This does the same thing.
207 matlab_print_info($filename, data => 1 );
208  
209 Print information about the contents of the matlab file C<$filename>,
210 including the name, dimension and class type of the variables.
211  
212 =cut
213  
214 sub matlab_print_info {
215 0 ▶ my $name = shift;
Subroutine matlab_print_info: not called CC=1 SCAR=6.9
Pod documented
216 0 my $mat = PDL::IO::Matlab->new($name, '<');
217 0 $mat->print_all_var_info(@_);
218 0 $mat->close;
219 }
220  
221 =head1 METHODS
222  
223 =head2 B<new>
224  
225 =head3 Usage
226  
227 # open for writing
228 $mat = PDL::IO::Matlab->new('file.dat', '>', {format => 'MAT5'});
229  
230 # default format is MAT5
231 $mat = PDL::IO::Matlab->new('file.dat', '>');
232  
233 # may use 'w' or '>'
234 $mat = PDL::IO::Matlab->new('file.dat', 'w');
235  
236 # supply header
237 $mat = PDL::IO::Matlab->new('file.dat', '>', { header => 'some text'} );
238  
239 # read-write with rw or <>
240 $mat = PDL::IO::Matlab->new('file.dat', 'rw');
241  
242 # open for reading
243 $mat = PDL::IO::Matlab->new('file.dat', '<');
244  
245 =head3 Options
246  
247 =over
248  
249 =item format
250  
251 Either C<'MAT5'> or C<'MAT73'>.
252  
253 =item compress
254  
255 Either C<1> for yes, or C<0> for no.
256  
257 =item header
258  
259 A header (a string) to write into the file.
260  
261 =item namekey
262  
263 A hash key that will be used to store the matlab name
264 for a variable read from a file in the header of an ndarray.
265 The default value is 'NAME'. Thus, the name can be accessed
266 via C<< $pdl->hdr->{NAME} >>.
267  
268 =item varbasew
269  
270 The base of the default matlab variable name that will be
271 written in the matlab file along with each ndarray. An
272 integer will be appended to the base name. This integer is
273 initialized to zero and is incremented after writing each
274 variable.
275  
276 =back
277  
278 The option C<compress> enables zlib compression if the zlib library
279 is available and if the data file format is C<'MAT5'>.
280  
281 =cut
282  
283 sub new {
284 7 ▶ my $class = shift;
Subroutine new: called CC=15 SCAR=33.1
Pod documented
285 7 my ($filename,$mode,$iopts) = @_;
286 7 my $opt = new PDL::Options(
287 {
288 format => undef,
289 header => undef,
290 namekey => 'NAME',
291 varbasew => 'variable',
292 onedw => 1,
293 onedr => 1,
294 compress => 0
295 });
296 7 ▶ $iopts ||= {};
Condition100%2/2
$iopts ||= {}
l!l
43
MC/DC100%1/1
$iopts ||= {}
$iopts
Truth table100%2/2
A
A= $iopts
Aresult
11
00
297  
298 7 my $obj = $opt->options($iopts);
299  
300 7 my %exobj = (
301 filename => undef,
302 mode => undef,
303 handle => undef,
304 wvarnum => 0,
305 );
306  
307 7 foreach (keys %exobj) { $obj->{$_} = $exobj{$_} };
308  
309 7 bless $obj, $class;
310  
311 7 ▶ $obj->set_filename($filename) if $filename;
Branch: 1 branch
BranchTrueFalseTotal
if $filename 7 0 7
312  
313 7 ▶ if ( defined $mode ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $mode) { } 7 0 7
elsif (defined $filename) { } 0 0 0
314 7 ▶ if ($mode eq 'r' or $mode eq '<') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'r' or $mode eq '<') { } 4 3 7
elsif ($mode eq 'w' or $mode eq '>') { } 3 0 3
elsif ($mode eq 'rw' or $mode eq '<>') { } 0 0 0
Condition66%2/3
$mode eq "r" or $mode eq "<"
l!l&&r!l&&!r
043
MC/DC50%1/2
$mode eq "r" or $mode eq "<"
$mode eq "r" $mode eq "<"
Truth table66%2/3
A or B
A= $mode eq "r"B= $mode eq "<"
ABresult
1X1
011
000
315 4 $obj->set_mode('r');
316 }
317 ▶ elsif ($mode eq 'w' or $mode eq '>') {
Condition33%1/3
$mode eq "w" or $mode eq ">"
l!l&&r!l&&!r
030
MC/DC0%0/2
$mode eq "w" or $mode eq ">"
$mode eq "w" $mode eq ">"
Truth table33%1/3
A or B
A= $mode eq "w"B= $mode eq ">"
ABresult
1X1
011
000
318 3 $obj->set_mode('w');
319 }
320 ▶ elsif ($mode eq 'rw' or $mode eq '<>') {
Condition0%0/3
$mode eq "rw" or $mode eq "<>"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "rw" or $mode eq "<>"
$mode eq "rw" $mode eq "<>"
Truth table0%0/3
A or B
A= $mode eq "rw"B= $mode eq "<>"
ABresult
1X1
011
000
321 0 $obj->set_mode('rw');
322 }
323 else {
324 0 barf "PDL::IO::Matlab::open unknown mode '$mode'";
325 }
326 }
327 elsif (defined $filename) {
328 0 barf("PDL::IO::Matlab::new filename given, but no access mode.");
329 }
330 barf("PDL::IO::Matlab::new unknown file format")
331 7 ▶ if defined $obj->{format} and not exists $Format_list{$obj->{format}};
Branch: 1 branch
BranchTrueFalseTotal
if defined $obj->{"format"} and not exists $Format_list{$obj->{"format"}} 0 7 7
Condition66%2/3
defined $obj->{"format"} and not exists $Format_list{$obj->{"format"}}
!ll&&!rl&&r
430
MC/DC0%0/2
defined $obj->{"format"} and not exists $Format_list{$obj->{"format"}}
defined $obj->{"format"} not exists $Format_list{$obj->{"format"}}
Truth table66%2/3
A and not(B)
A= defined $obj->{"format"}B= not exists $Format_list{$obj->{"format"}}
ABresult
0X0
100
111
332 7 ▶ $obj->open() if defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename 7 0 7
333 7 $obj;
334 }
335  
336 # may want to keep track of state at some point,
337 # an automatically close.
338 sub DESTROY {
339 7 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
340 # $self->close;
341 }
342  
343 sub open {
344 7 ▶ my $self = shift;
Subroutine open: called CC=9 SCAR=23.6
Pod undocumented
345 7 my $mode = $self->get_mode();
346 7 my $filename = $self->get_filename();
347 7 my $handle;
348 7 ▶ if ( $mode eq 'r' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'r') { } 4 3 7
elsif ($mode eq 'w') { } 3 0 3
elsif ($mode eq 'rw') { } 0 0 0
349 4 $handle = _mat_open_read($filename);
350 }
351 elsif ( $mode eq 'w' ) {
352 3 ▶ $self->get_format || $self->set_format('MAT5');
Branch: 1 branch
BranchTrueFalseTotal
$self->get_format or $self->set_format("MAT5") 0 3 3
353 3 my $header = $self->get_header();
354 3 ▶ my $header_flag = defined $header ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $header ? : 0 3 3
355 3 ▶ $header = '' unless defined $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 3 0 3
356 $handle = _mat_create_ver(
357 3 $filename, $header, $Format_list{$self->get_format}, $header_flag);
358 }
359 elsif ( $mode eq 'rw' ) {
360 0 $handle = _mat_open_read_write($filename);
361 }
362 else {
363 0 barf "PDL::IO::Matlab::open unknown mode '$mode'";
364 }
365 7 ▶ barf "PDL::IO::Matlab::open Can't open '$filename' in mode $mode" unless $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 0 7 7
366 7 $self->set_handle($handle);
367 7 ▶ $self->get_format || $self->set_format($self->get_version);
Branch: 1 branch
BranchTrueFalseTotal
$self->get_format or $self->set_format($self->get_version) 4 3 7
368 7 $self;
369 }
370  
371 =head2 B<close>
372  
373 =head3 Usage
374  
375 $mat->close;
376  
377 Close matlab file and free memory associated with C<$mat>.
378  
379 =cut
380  
381 sub close {
382 7 ▶ my $self = shift;
Subroutine close: called CC=1 SCAR=0.0
Pod documented
383 7 _mat_close($self->get_handle() );
384 7 $self;
385 }
386  
387 =head2 B<read_next>
388  
389 =head3 Usage
390  
391 my $x = $mat->read_next;
392 print "End of file\n" unless ref($x);
393  
394 my ($err,$x) = $mat->read_next;
395 print "End of file\n" if $err;
396  
397 Read one pdl from file associated with object C<$mat>.
398  
399 =cut
400  
401 sub read_next {
402 25 ▶ my $self = shift;
Subroutine read_next: called CC=3 SCAR=11.0
Pod documented
403 25 my ($pdl,$matlab_name) = _convert_next_matvar_to_pdl($self->get_handle,
404 $self->get_onedr);
405 25 ▶ my $err = ref($pdl) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $pdl ? : 21 4 25
406 25 ▶ $pdl->hdr->{$self->get_namekey} = $matlab_name if defined $matlab_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $matlab_name 21 4 25
407 25 return ($err,$pdl);
408 }
409  
410 =head2 B<read_all>
411  
412 =head3 Usage
413  
414 my @pdls = $mat->read_all;
415  
416 Read all remaining pdls from file associated with object C<$mat>.
417  
418 =cut
419  
420 sub read_all {
421 3 ▶ my $self = shift;
Subroutine read_all: called CC=2 SCAR=6.9
Pod documented
422 3 my @res;
423 3 while(1) {
424 17 my ($err,$pdl) = read_next($self);
425 17 ▶ last if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 14 17
426 14 push @res, $pdl;
427 }
428 3 @res;
429 }
430  
431 =head2 B<write>
432  
433 =head3 Usage
434  
435 $x2->hdr->{NAME} = 'variablename';
436  
437 $mat->write($x1,$x2,...);
438  
439 $mat->write($x1,$x2,...,{OPTIONS});
440  
441 Append pdls to open file associated with C<$mat>.
442  
443 If an ndarray has a matlab name stored in the header
444 it will be used as the matlab name written to the file
445 with this ndarray. The key is in C<< $pdl->{namekey} >>,
446 with default value C<'NAME'>. If the name is not in
447 the ndarray's header, then a default value will be used.
448  
449 =head3 Options
450  
451 =over
452  
453 =item onedw
454  
455 In order to write a file that is compatible with Matlab and Octave,
456 C<onedw> must be either C<1> or C<2>. If C<onedw> is C<1> then a 1-d
457 pdl of length n is written as a as an C<nx1> pdl (a C<1xn> matlab
458 variable). If C<onedw> is C<2> then the output ndarray is C<1xn> and
459 the matlab variable C<nx1>. If C<onedw> is zero (the default), then
460 the 1-d pdl is written as a 1-d ndarray. In the last case, Octave will
461 print an error and fail to read the variable.
462  
463 =item compress
464  
465 If C<compress> is C<1> then zlib compression is used, if the library
466 is available and if the format is C<'MAT5'>.
467  
468 =back
469  
470 =cut
471  
472 sub _make_write_var_name {
473 9 ▶ my $self = shift;
Subroutine _make_write_var_name: called CC=1 SCAR=0.0
474 9 my $varname = $self->get_varbasew . $self->get_wvarnum;
475 9 $self->set_wvarnum($self->get_wvarnum + 1);
476 9 $varname;
477 }
478  
479 sub write {
480 8 ▶ my $self = shift;
Subroutine write: called CC=10 SCAR=25.0
Pod documented
481 8 my @pdls;
482 my @hashes;
483 8 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 8 17
484 9 my $arg = shift;
485 9 ▶ if (ref($arg) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 0 9 9
486 0 push @hashes, $arg;
487 }
488 else {
489 9 push @pdls, $arg;
490 }
491 }
492 # my %opts = parse( {onedw => 0 }, @hashes ? $hashes[0] : {} );
493 8 ▶ my $opts = @hashes ? $hashes[0] : {} ;
Branch: 1 branch
BranchTrueFalseTotal
@hashes ? : 0 8 8
494 8 ▶ my $onedw = exists $opts->{onedw} ? $opts->{onedw} : $self->get_onedw;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'onedw'} ? : 0 8 8
495 8 ▶ $onedw = 1 if $onedw == 0 and $self->get_format eq 'MAT73'; # else crash
Branch: 1 branch
BranchTrueFalseTotal
if $onedw == 0 and $self->get_format eq "MAT73" 0 8 8
Condition33%1/3
$onedw == 0 and $self->get_format eq "MAT73"
!ll&&!rl&&r
800
MC/DC0%0/2
$onedw == 0 and $self->get_format eq "MAT73"
$onedw == 0 $self->get_format eq "MAT73"
Truth table33%1/3
A and B
A= $onedw == 0B= $self->get_format eq "MAT73"
ABresult
0X0
100
111
496 8 ▶ my $compress = exists $opts->{compress} ? $opts->{compress} : $self->get_compress;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'compress'} ? : 0 8 8
497 8 foreach (@pdls) {
498 my $name = exists $_->hdr->{$self->get_namekey} ?
499 9 ▶ $_->hdr->{$self->get_namekey} : $self->_make_write_var_name;
Branch: 1 branch
BranchTrueFalseTotal
exists $_->hdr->{$self->get_namekey} ? : 0 9 9
500 9 _write_pdl_to_matlab_file($self->get_handle,$_, $name, $onedw, $compress);
501 }
502 8 return $self;
503 }
504  
505 =head2 B<rewind>
506  
507 =head3 Usage
508  
509 $mat->rewind
510  
511 Reset pointer to the head of the file.
512  
513 =cut
514  
515 sub rewind {
516 1 ▶ my $self = shift;
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
517 1 _mat_rewind($self->get_handle);
518 }
519  
520 =head2 B<get_filename>
521  
522 =head3 Usage
523  
524 $mat->get_filename
525  
526 Return name of file associated with C<$mat>.
527  
528 =cut
529  
530 =head2 B<get_header>
531  
532 =head3 Usage
533  
534 $mat->get_header
535  
536 Return the header string from the matlab data file associated with
537 C<$mat>.
538  
539 =cut
540  
541 sub get_header {
542 4 ▶ my $self = shift;
Subroutine get_header: called CC=3 SCAR=11.1
Pod documented
543 return defined $self->{header} ?
544 $self->{header} : defined $self->{handle} ?
545 4 ▶ _mat_get_header($self->{handle}) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'header'} ? : 0 4 4
defined $self->{'handle'} ? : 1 3 4
546 }
547  
548 sub set_header {
549 0 ▶ my $self = shift;
Subroutine set_header: not called CC=1 SCAR=6.9
Pod undocumented
550 0 $self->{header} = shift;
551 }
552  
553 =head2 B<get_format>
554  
555 =head3 Usage
556  
557 $mat->get_format
558  
559 Return matlab data file format for file associated with
560 C<$mat>. One of C<'MAT4'>, C<'MAT5'>, or C<'MAT73'>.
561  
562 =cut
563  
564 # I am not using this ??
565 # version here means file format, use get_format instead
566 sub get_version {
567 5 ▶ my $self = shift;
Subroutine get_version: called CC=1 SCAR=0.0
Pod undocumented
568 5 my $val = _mat_get_version($self->get_handle);
569 5 $Inv_format_list{$val};
570 }
571  
572 =head2 B<print_all_var_info>
573  
574 =head3 Usage
575  
576 $mat->print_all_var_info;
577  
578 # also print a small amount of data from each variable.
579 $mat->print_all_var_info( data => 1 );
580  
581 Print a summary of all data in the file associated
582 with C<$mat> (starting from the next unread variable.)
583  
584 =cut
585  
586 sub print_all_var_info {
587 0 ▶ my $self = shift;
Subroutine print_all_var_info: not called CC=4 SCAR=30.0
Pod documented
588 0 my $len = scalar(@_);
589 0 my $user_options = {};
590 0 ▶ if ( $len == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len == 1) { } 0 0 0
elsif ($len > 1) { } 0 0 0
591 0 $user_options = $_[0];
592 }
593 elsif ( $len > 1 ) {
594 0 my %user_option_hash = @_;
595 0 $user_options = \%user_option_hash;
596 }
597 0 my %opts = parse( {data => 0} , $user_options);
598 0 ▶ my $printdata = $opts{data} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'data'} ? : 0 0 0
599 0 my $handle = $self->get_handle;
600 0 _extra_matio_print_all_var_info($handle,$printdata);
601 }
602  
603 EO_MIDDLE
604  
605 sub write_accessors {
606 # !e means barf in get_property if property is not set.
607 my @funcs = qw(
608 handle!e mode!e filename!e format
609 varbasew!e onedw!e onedr!e namekey!e wvarnum!e compress!e);
610 my $s = '';
611 my @accessors;
612 my $errs;
613 foreach (@funcs) {
614 $errs = s/!e// ?
615 "\n barf 'PDL::IO::Matlab::get_$_: $_ not defined.' unless
616 defined \$self->{$_};" : '';
617 my $getter = "get_$_";
618 my $setter = "set_$_";
619 push @accessors, $getter;
620 push @accessors, $setter;
621 $s .= "\n
622 sub $getter {
623 my \$self = shift;$errs
624 \$self->{$_};
625 }
626  
627 sub $setter {
628 my \$self = shift;
629 \$self->{$_} = shift;
630 }
631 "
632 }
633 $s .= "
634  
635 =head1 ACCESSOR METHODS
636  
637 The following are additional accessor methods for the matlab file objects
638 PDL::IO::Matlab.
639  
640 "
641 . join(' ',@accessors) .
642  
643 "
644 =cut
645 ";
646  
647 $s;
648 }
649  
650 pp_addpm({At => 'Middle'}, write_accessors() );
651 47 ▶ 
Subroutine get_handle: called CC=2 SCAR=7.1
Pod undocumented
652  
653 47 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"handle"} 0 47 47
654 47 pp_addpm({At => 'Middle'}, <<'EO_MIDDLE_two');
655  
656  
657 =head1 CAVEATS
658 7 ▶ 
Subroutine set_handle: called CC=1 SCAR=0.0
Pod undocumented
659 7 =head2 complicating factors
660  
661 There are two complicating factors when using matlab files with PDL.
662 First, matlab does not support one-dimensional vectors. Thus, a 1-d pdl
663 must be represented as either a C<1 x n> of a C<n x 1> matlab variable. Second,
664 7 ▶matlab stores matrices in column-major order, while pdl stores them
Subroutine get_mode: called CC=2 SCAR=7.1
Pod undocumented
665 in row-major order.
666 7 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"mode"} 0 7 7
667 7 =over
668  
669 =item B<one-dimensional pdls>
670  
671 7 ▶You can write 1-d pdls to a file with this module. This module can then read the
Subroutine set_mode: called CC=1 SCAR=0.0
Pod undocumented
672 7 file. But, Octave will fail to read the file and print an error message.
673 See L</B<write>> for how this is handled.
674  
675 =item B<column- vs. row major>
676  
677 7 ▶Data written by Octave (PDL) will be read by PDL (Octave) with indices transposed.
Subroutine get_filename: called CC=2 SCAR=7.1
Pod documented
678 On the todo list is an option to physically or logically transpose the data on
679 7 ▶reading and writing.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"filename"} 0 7 7
680 7  
681 =item B<Octave requires distinct matlab variable names>
682  
683 With this module, you may write more than one
684 7 ▶variable, each with the same name, (the matlab name; not the
Subroutine set_filename: called CC=1 SCAR=0.0
Pod undocumented
685 7 pdl identifier, or variable, name), to a file in MAT5
686 format. This module is then able to read all pdls from this file.
687 But, Octave, when reading this file, will overwrite all but
688 the last occurrence of the variable with the last
689 occurrence. See the method L</B<write>>.
690 13 ▶ 
Subroutine get_format: called CC=1 SCAR=0.0
Pod documented
691 13 Trying to write two pdls with the same matlab variable name in MAT73 format will cause
692 an error.
693  
694 =back
695 4 ▶ 
Subroutine set_format: called CC=1 SCAR=0.0
Pod undocumented
696 4 =head2 other missing features, bugs
697  
698 When trying to read an unsupported matlab data type from a file, this module will
699 throw an error. Supporting other data types or optionally skipping them is on
700 the todo list.
701 9 ▶ 
Subroutine get_varbasew: called CC=2 SCAR=7.1
Pod undocumented
702 Random access of variables in a file is on the todo list. The underlying B<matio>
703 9 ▶library supports this.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"varbasew"} 0 9 9
704 9  
705 This module is currently built with some hardcoded data from a PDL installation, that
706 may contain platform-specific (linux) features. It may fail to
707 build or function correctly when used on other platforms.
708 0 ▶ 
Subroutine set_varbasew: not called CC=1 SCAR=6.9
Pod undocumented
709 0 =head1 AUTHOR
710  
711 John Lapeyre, C<< <jlapeyre at cpan.org> >>
712  
713 The matio library was written by Christopher C. Hulbert.
714 8 ▶ 
Subroutine get_onedw: called CC=2 SCAR=7.1
Pod undocumented
715 =head1 LICENSE AND COPYRIGHT
716 8 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"onedw"} 0 8 8
717 8 Copyright 2012 John Lapeyre.
718  
719 This program is free software; you can redistribute it and/or modify it
720 under the terms of either: the GNU General Public License as published
721 0 ▶by the Free Software Foundation; or the Artistic License.
Subroutine set_onedw: not called CC=1 SCAR=6.9
Pod undocumented
722 0  
723 See http://dev.perl.org/licenses/ for more information.
724  
725 The matio library included here is
726 Copyright 2011 Christopher C. Hulbert. All rights reserved.
727 25 ▶See the file matio-1.5/COPYING in the source distribution
Subroutine get_onedr: called CC=2 SCAR=7.1
Pod undocumented
728 of this module.
729 25 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"onedr"} 0 25 25
730 25 =cut
731  
732 # broken
733 #sub print_all_var_info_new {
734 0 ▶# my $self = shift;
Subroutine set_onedr: not called CC=1 SCAR=6.9
Pod undocumented
735 0 # my $handle = $self->get_handle;
736 # _extra_matio_print_all_var_info($handle,1,10,10);
737 #}
738  
739 ###########################################################################
740 30 ▶ 
Subroutine get_namekey: called CC=2 SCAR=7.1
Pod undocumented
741 EO_MIDDLE_two
742 30 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"namekey"} 0 30 30
743 30  
744 pp_addhdr( qq{
745 #include <stdio.h>
746 #include <stdlib.h>
747 0 ▶#ifdef DISTRO_MATIO
Subroutine set_namekey: not called CC=1 SCAR=6.9
Pod undocumented
748 0 #include "$Matio_src/matio.h"
749 #else
750 #include <matio.h>
751 #endif
752 //#include "$Matio_src/matio_private.h"
753 18 ▶//#include "$Matio_src/mat5.h"
Subroutine get_wvarnum: called CC=2 SCAR=7.1
Pod undocumented
754 //#include "$Matio_src/mat4.h"
755 18 ▶//#include "$Matio_src/mat73.h"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"wvarnum"} 0 18 18
756 18 //#include "mat_var_print.c"
757 #include "extra_matio.c"
758 #include "convert_matvar_pdl.c"
759 });
760 9 ▶ 
Subroutine set_wvarnum: called CC=1 SCAR=0.0
Pod undocumented
761 9 pp_add_exported qw( matlab_read matlab_write matlab_print_info );
762  
763 # The line MODULE = ... is also added by PP, so this
764 # is redundant
765  
766 8 ▶pp_addxs ( '', '
Subroutine get_compress: called CC=2 SCAR=7.1
Pod undocumented
767  
768 8 ▶MODULE = PDL::IO::Matlab PACKAGE = PDL::IO::Matlab
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"compress"} 0 8 8
769 8  
770 mat_t *
771 _mat_open_read( filename )
772 char * filename
773 0 ▶ CODE:
Subroutine set_compress: not called CC=1 SCAR=6.9
Pod undocumented
774 0 RETVAL = Mat_Open(filename,MAT_ACC_RDONLY);
775 OUTPUT:
776 RETVAL
777  
778 mat_t *
779 _mat_open_read_write( filename )
780 char * filename
781 CODE:
782 RETVAL = Mat_Open(filename,MAT_ACC_RDWR);
783 OUTPUT:
784 RETVAL
785  
786 int
787 _mat_close( handle )
788 mat_t * handle
789 CODE:
790 /* always returns 0 */
791 RETVAL = Mat_Close(handle);
792 OUTPUT:
793 RETVAL
794  
795 int
796 _have_hdf5()
797 CODE:
798 #if defined(HAVE_HDF5) && HAVE_HDF5 && defined(MAT73) && MAT73
799 RETVAL = 1;
800 #else
801 RETVAL = 0;
802 #endif
803 OUTPUT:
804 RETVAL
805  
806 ');
807  
808  
809 pp_addxs (<<EOF
810  
811 mat_t *
812 _mat_create_ver( matname, hdr_str, mat_file_ver_in, header_flag)
813 char * matname
814 char * hdr_str
815 int mat_file_ver_in
816 int header_flag
817 CODE:
818 /* following should be written w/o conditionals ! */
819 enum mat_ft mat_file_ver;
820 switch (mat_file_ver_in)
821 {
822 #if defined(MAT73) && MAT73
823 case $MAT_FT_MAT73:
824 {
825 mat_file_ver = MAT_FT_MAT73;
826 break;
827 }
828 #endif
829 case $MAT_FT_MAT5:
830 {
831 mat_file_ver = MAT_FT_MAT5;
832 break;
833 }
834 case $MAT_FT_MAT4:
835 {
836 barf("PDL::IO::Matlab::open Writing in format version MAT4 not supported.");
837 }
838 default:
839 barf("PDL::IO::Matlab::open unknown file format.");
840 }
841 if (header_flag)
842 RETVAL = Mat_CreateVer(matname, hdr_str, mat_file_ver);
843 else RETVAL = Mat_CreateVer(matname, (void *) NULL, mat_file_ver);
844 OUTPUT:
845 RETVAL
846 EOF
847 );
848  
849 pp_addxs ( '', "
850  
851 int
852 _mat_get_version(matfp)
853 mat_t * matfp
854 CODE:
855 enum mat_ft mat_file_ver;
856 int retval;
857 mat_file_ver = Mat_GetVersion(matfp);
858 /* fprintf(stderr, \"in C vers %d\", mat_file_ver); */
859 /* This could be done much more easily ! */
860 switch (mat_file_ver)
861 {
862 case MAT_FT_MAT73:
863 {
864 retval= $MAT_FT_MAT73;
865 break;
866 }
867 case MAT_FT_MAT5:
868 {
869 retval= $MAT_FT_MAT5;
870 break;
871 }
872 case MAT_FT_MAT4:
873 {
874 retval= $MAT_FT_MAT4;
875 break;
876 }
877 default:
878 {
879 barf(\"PDL::IO::Matlab::get_version returned unknown version.\");
880 }
881 }
882 RETVAL = retval;
883 OUTPUT:
884 RETVAL
885 ");
886  
887 pp_addxs( '', '
888  
889 void
890 _read_data_all (mat, matvar)
891 mat_t * mat
892 matvar_t * matvar
893 CODE:
894 Mat_VarReadDataAll(mat,matvar);
895  
896 void
897 get_library_version()
898 INIT:
899 int major,minor,release;
900  
901 PPCODE:
902 Mat_GetLibraryVersion(&major, &minor, &release);
903 XPUSHs(sv_2mortal(newSVnv(major)));
904 XPUSHs(sv_2mortal(newSVnv(minor)));
905 XPUSHs(sv_2mortal(newSVnv(release)));
906  
907 int
908 _mat_rewind(mat)
909 mat_t * mat
910 CODE:
911 RETVAL = Mat_Rewind(mat);
912 if (RETVAL == -1) barf("PDL::IO::Matlab::rewind Bad filehandle");
913 OUTPUT:
914 RETVAL
915  
916 char *
917 _mat_get_header(mat)
918 mat_t * mat
919 CODE:
920 RETVAL = (char *)Mat_GetHeader(mat);
921 OUTPUT:
922 RETVAL
923  
924 char *
925 _mat_get_filename(mat)
926 mat_t * mat
927 CODE:
928 RETVAL = (char *)Mat_GetFilename(mat);
929 OUTPUT:
930 RETVAL
931  
932 ');
933  
934 =for comment
935  
936 int
937 _my_mat_feof(mat)
938 mat_t * mat
939 CODE:
940 RETVAL = feof(mat->fp);
941 OUTPUT:
942 RETVAL
943  
944 =cut
945  
946  
947 pp_addxs ( '', '
948  
949 void
950 _extra_matio_print_all_var_info (matfp,printdata)
951 mat_t * matfp
952 int printdata
953 CODE:
954 extra_matio_print_all_var_info(matfp,printdata);
955  
956 void
957 _convert_next_matvar_to_pdl(matfp,onedr)
958 mat_t * matfp
959 int onedr
960 PPCODE:
961 pdl * retpdl;
962 matvar_t * matvar;
963 retpdl = convert_next_matvar_to_pdl(matfp, &matvar, onedr);
964 SV * s = sv_newmortal();
965 SV * p = sv_newmortal();
966 if (retpdl != NULL) PDL->SetSV_PDL(p,retpdl);
967 if (matvar != NULL) {
968 sv_setpv(s, matvar->name);
969 Mat_VarFree(matvar);
970 }
971 XPUSHs(p);
972 XPUSHs(s);
973  
974 int
975 _write_pdl_to_matlab_file (mat, ndarray, varname, oned, compress)
976 mat_t * mat
977 pdl * ndarray
978 char * varname
979 int oned
980 int compress
981 CODE:
982 RETVAL = write_pdl_to_matlab_file (mat, ndarray, varname, oned, compress);
983 OUTPUT:
984 RETVAL
985  
986  
987 ');
988  
989 pp_done();
990  
991  
992 1;
993  
994 # LocalWords: setversion Matio src Enums enum fh matio EO
995 # LocalWords: addpm TOPMATTER Matlab pdls OO multi PDL qw
996 # LocalWords: matlab pdl dat Inv filename wantarray rw eq
997 # LocalWords: iopts undef wvarname wvarnum elsif ver cpan
998 # LocalWords: matvar varname foreach printdata Lapeyre qq
999 # LocalWords: jlapeyre Hulbert addhdr stdlib addxs RETVAL
1000 # LocalWords: ACC RDONLY RDWR matname hdr str CreateVer
1001 # LocalWords: matfp retval GetVersion fprintf stderr vers
1002 # LocalWords: VarReadDataAll INIT PPCODE XPUSHs sv feof
1003 # LocalWords: GetLibraryVersion newSVnv filehandle fp EOF
1004 # LocalWords: newmortal SetSV namekey varbasew exobj todo
1005 # LocalWords: hardcoded retpdl setpv VarFree disa
↑ Summary extra_matio.c »