| 1 |
##-*- Mode: Perl -*- |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '0.008'; |
| 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 |
|
| 78 |
use strict; |
| 79 |
use warnings; |
| 80 |
|
| 81 |
use PDL::LiteF; |
| 82 |
use PDL::NiceSlice; |
| 83 |
use PDL::Options; |
| 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 |
|
| 132 |
sub matlab_read { |
| 133 |
my ($filename,$opts) = @_; |
| 134 |
my $mat = PDL::IO::Matlab->new($filename, '<', $opts || {}); |
| 135 |
my @res = $mat->read_all; |
| 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 |
=cut |
| 166 |
|
| 167 |
sub matlab_write { |
| 168 |
my @strings; |
| 169 |
my @hashes; |
| 170 |
my @refs; |
| 171 |
while(@_) { |
| 172 |
my $v = shift; |
| 173 |
if ( ref($v) ) { |
| 174 |
if ( ref($v) eq 'HASH' ) { |
| 175 |
push @hashes, $v; |
| 176 |
} |
| 177 |
else { |
| 178 |
push @refs, $v; |
| 179 |
} |
| 180 |
} |
| 181 |
else { |
| 182 |
push @strings, $v; |
| 183 |
} |
| 184 |
} |
| 185 |
barf 'matlab_write: ' . scalar(@strings) . |
| 186 |
' string arguments given. One or two expected.' |
| 187 |
if @strings < 1 or @strings > 2 ; |
| 188 |
my $filename = $strings[0]; |
| 189 |
my $format = $strings[1] || 'MAT5'; |
| 190 |
my $opth = { format => $format }; |
| 191 |
foreach (keys %{$hashes[0]}) { $opth->{$_} = $hashes[0]->{$_} } |
| 192 |
my $mat = PDL::IO::Matlab->new($filename, '>', $opth); |
| 193 |
$mat->write(@refs) if @refs; |
| 194 |
$mat->close; |
| 195 |
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 |
my $name = shift; |
| 216 |
my $mat = PDL::IO::Matlab->new($name, '<'); |
| 217 |
$mat->print_all_var_info(@_); |
| 218 |
$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 |
my $class = shift; |
| 285 |
my ($filename,$mode,$iopts) = @_; |
| 286 |
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 |
$iopts ||= {}; |
| 297 |
|
| 298 |
my $obj = $opt->options($iopts); |
| 299 |
|
| 300 |
my %exobj = ( |
| 301 |
filename => undef, |
| 302 |
mode => undef, |
| 303 |
handle => undef, |
| 304 |
wvarnum => 0, |
| 305 |
); |
| 306 |
|
| 307 |
foreach (keys %exobj) { $obj->{$_} = $exobj{$_} }; |
| 308 |
|
| 309 |
bless $obj, $class; |
| 310 |
|
| 311 |
$obj->set_filename($filename) if $filename; |
| 312 |
|
| 313 |
if ( defined $mode ) { |
| 314 |
if ($mode eq 'r' or $mode eq '<') { |
| 315 |
$obj->set_mode('r'); |
| 316 |
} |
| 317 |
elsif ($mode eq 'w' or $mode eq '>') { |
| 318 |
$obj->set_mode('w'); |
| 319 |
} |
| 320 |
elsif ($mode eq 'rw' or $mode eq '<>') { |
| 321 |
$obj->set_mode('rw'); |
| 322 |
} |
| 323 |
else { |
| 324 |
barf "PDL::IO::Matlab::open unknown mode '$mode'"; |
| 325 |
} |
| 326 |
} |
| 327 |
elsif (defined $filename) { |
| 328 |
barf("PDL::IO::Matlab::new filename given, but no access mode."); |
| 329 |
} |
| 330 |
barf("PDL::IO::Matlab::new unknown file format") |
| 331 |
if defined $obj->{format} and not exists $Format_list{$obj->{format}}; |
| 332 |
$obj->open() if defined $filename; |
| 333 |
$obj; |
| 334 |
} |
| 335 |
|
| 336 |
# may want to keep track of state at some point, |
| 337 |
# an automatically close. |
| 338 |
sub DESTROY { |
| 339 |
my $self = shift; |
| 340 |
# $self->close; |
| 341 |
} |
| 342 |
|
| 343 |
sub open { |
| 344 |
my $self = shift; |
| 345 |
my $mode = $self->get_mode(); |
| 346 |
my $filename = $self->get_filename(); |
| 347 |
my $handle; |
| 348 |
if ( $mode eq 'r' ) { |
| 349 |
$handle = _mat_open_read($filename); |
| 350 |
} |
| 351 |
elsif ( $mode eq 'w' ) { |
| 352 |
$self->get_format || $self->set_format('MAT5'); |
| 353 |
my $header = $self->get_header(); |
| 354 |
my $header_flag = defined $header ? 1 : 0; |
| 355 |
$header = '' unless defined $header; |
| 356 |
$handle = _mat_create_ver( |
| 357 |
$filename, $header, $Format_list{$self->get_format}, $header_flag); |
| 358 |
} |
| 359 |
elsif ( $mode eq 'rw' ) { |
| 360 |
$handle = _mat_open_read_write($filename); |
| 361 |
} |
| 362 |
else { |
| 363 |
barf "PDL::IO::Matlab::open unknown mode '$mode'"; |
| 364 |
} |
| 365 |
barf "PDL::IO::Matlab::open Can't open '$filename' in mode $mode" unless $handle; |
| 366 |
$self->set_handle($handle); |
| 367 |
$self->get_format || $self->set_format($self->get_version); |
| 368 |
$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 |
my $self = shift; |
| 383 |
_mat_close($self->get_handle() ); |
| 384 |
$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 |
my $self = shift; |
| 403 |
my ($pdl,$matlab_name) = _convert_next_matvar_to_pdl($self->get_handle, |
| 404 |
$self->get_onedr); |
| 405 |
my $err = ref($pdl) ? 0 : 1; |
| 406 |
$pdl->hdr->{$self->get_namekey} = $matlab_name if defined $matlab_name; |
| 407 |
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 |
my $self = shift; |
| 422 |
my @res; |
| 423 |
while(1) { |
| 424 |
my ($err,$pdl) = read_next($self); |
| 425 |
last if $err; |
| 426 |
push @res, $pdl; |
| 427 |
} |
| 428 |
@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 |
my $self = shift; |
| 474 |
my $varname = $self->get_varbasew . $self->get_wvarnum; |
| 475 |
$self->set_wvarnum($self->get_wvarnum + 1); |
| 476 |
$varname; |
| 477 |
} |
| 478 |
|
| 479 |
sub write { |
| 480 |
my $self = shift; |
| 481 |
my @pdls; |
| 482 |
my @hashes; |
| 483 |
while (@_) { |
| 484 |
my $arg = shift; |
| 485 |
if (ref($arg) eq 'HASH') { |
| 486 |
push @hashes, $arg; |
| 487 |
} |
| 488 |
else { |
| 489 |
push @pdls, $arg; |
| 490 |
} |
| 491 |
} |
| 492 |
# my %opts = parse( {onedw => 0 }, @hashes ? $hashes[0] : {} ); |
| 493 |
my $opts = @hashes ? $hashes[0] : {} ; |
| 494 |
my $onedw = exists $opts->{onedw} ? $opts->{onedw} : $self->get_onedw; |
| 495 |
$onedw = 1 if $onedw == 0 and $self->get_format eq 'MAT73'; # else crash |
| 496 |
my $compress = exists $opts->{compress} ? $opts->{compress} : $self->get_compress; |
| 497 |
foreach (@pdls) { |
| 498 |
my $name = exists $_->hdr->{$self->get_namekey} ? |
| 499 |
$_->hdr->{$self->get_namekey} : $self->_make_write_var_name; |
| 500 |
_write_pdl_to_matlab_file($self->get_handle,$_, $name, $onedw, $compress); |
| 501 |
} |
| 502 |
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 |
my $self = shift; |
| 517 |
_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 |
my $self = shift; |
| 543 |
return defined $self->{header} ? |
| 544 |
$self->{header} : defined $self->{handle} ? |
| 545 |
_mat_get_header($self->{handle}) : undef; |
| 546 |
} |
| 547 |
|
| 548 |
sub set_header { |
| 549 |
my $self = shift; |
| 550 |
$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 |
my $self = shift; |
| 568 |
my $val = _mat_get_version($self->get_handle); |
| 569 |
$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 |
my $self = shift; |
| 588 |
my $len = scalar(@_); |
| 589 |
my $user_options = {}; |
| 590 |
if ( $len == 1 ) { |
| 591 |
$user_options = $_[0]; |
| 592 |
} |
| 593 |
elsif ( $len > 1 ) { |
| 594 |
my %user_option_hash = @_; |
| 595 |
$user_options = \%user_option_hash; |
| 596 |
} |
| 597 |
my %opts = parse( {data => 0} , $user_options); |
| 598 |
my $printdata = $opts{data} ? 1 : 0; |
| 599 |
my $handle = $self->get_handle; |
| 600 |
_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 |
|
| 652 |
|
| 653 |
|
| 654 |
pp_addpm({At => 'Middle'}, <<'EO_MIDDLE_two'); |
| 655 |
|
| 656 |
|
| 657 |
=head1 CAVEATS |
| 658 |
|
| 659 |
=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 |
matlab stores matrices in column-major order, while pdl stores them |
| 665 |
in row-major order. |
| 666 |
|
| 667 |
=over |
| 668 |
|
| 669 |
=item B<one-dimensional pdls> |
| 670 |
|
| 671 |
You can write 1-d pdls to a file with this module. This module can then read the |
| 672 |
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 |
Data written by Octave (PDL) will be read by PDL (Octave) with indices transposed. |
| 678 |
On the todo list is an option to physically or logically transpose the data on |
| 679 |
reading and writing. |
| 680 |
|
| 681 |
=item B<Octave requires distinct matlab variable names> |
| 682 |
|
| 683 |
With this module, you may write more than one |
| 684 |
variable, each with the same name, (the matlab name; not the |
| 685 |
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 |
|
| 691 |
Trying to write two pdls with the same matlab variable name in MAT73 format will cause |
| 692 |
an error. |
| 693 |
|
| 694 |
=back |
| 695 |
|
| 696 |
=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 |
|
| 702 |
Random access of variables in a file is on the todo list. The underlying B<matio> |
| 703 |
library supports this. |
| 704 |
|
| 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 |
|
| 709 |
=head1 AUTHOR |
| 710 |
|
| 711 |
John Lapeyre, C<< <jlapeyre at cpan.org> >> |
| 712 |
|
| 713 |
The matio library was written by Christopher C. Hulbert. |
| 714 |
|
| 715 |
=head1 LICENSE AND COPYRIGHT |
| 716 |
|
| 717 |
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 |
by the Free Software Foundation; or the Artistic License. |
| 722 |
|
| 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 |
See the file matio-1.5/COPYING in the source distribution |
| 728 |
of this module. |
| 729 |
|
| 730 |
=cut |
| 731 |
|
| 732 |
# broken |
| 733 |
#sub print_all_var_info_new { |
| 734 |
# my $self = shift; |
| 735 |
# my $handle = $self->get_handle; |
| 736 |
# _extra_matio_print_all_var_info($handle,1,10,10); |
| 737 |
#} |
| 738 |
|
| 739 |
########################################################################### |
| 740 |
|
| 741 |
EO_MIDDLE_two |
| 742 |
|
| 743 |
|
| 744 |
pp_addhdr( qq{ |
| 745 |
#include <stdio.h> |
| 746 |
#include <stdlib.h> |
| 747 |
#ifdef DISTRO_MATIO |
| 748 |
#include "$Matio_src/matio.h" |
| 749 |
#else |
| 750 |
#include <matio.h> |
| 751 |
#endif |
| 752 |
//#include "$Matio_src/matio_private.h" |
| 753 |
//#include "$Matio_src/mat5.h" |
| 754 |
//#include "$Matio_src/mat4.h" |
| 755 |
//#include "$Matio_src/mat73.h" |
| 756 |
//#include "mat_var_print.c" |
| 757 |
#include "extra_matio.c" |
| 758 |
#include "convert_matvar_pdl.c" |
| 759 |
}); |
| 760 |
|
| 761 |
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 |
pp_addxs ( '', ' |
| 767 |
|
| 768 |
MODULE = PDL::IO::Matlab PACKAGE = PDL::IO::Matlab |
| 769 |
|
| 770 |
mat_t * |
| 771 |
_mat_open_read( filename ) |
| 772 |
char * filename |
| 773 |
CODE: |
| 774 |
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 |