Debian Codegraph @

README.pod

raw
1 =head1 NAME
2
3 PDL::IO::Matlab -- Read and write Matlab format data files.
4
5 =head1 DESCRIPTION
6
7 This module provides routines to read and write pdls to and from
8 data files in Matlab formats. The module uses the matio C library.
9 Both functional and OO interface are provided.
10
11 Only real, multi-dimensional arrays corresponding to PDL data types are supported.
12 Compression for both reading and writing is supported.
13
14 See the section L</CAVEATS> for important information on potential problems when using
15 this module.
16
17 =head1 SYNOPSIS
18
19 use PDL;
20 use PDL::IO::Matlab qw( matlab_read matlab_write matlab_print_info);
21
22 # write two pdls in matlab 5 format
23 matlab_write('file.dat', $x, $y);
24
25 # read an array of ndarrays
26 # from file in matlab 4, 5, or 7.3 format.
27 my @pdls = matlab_read('file.dat');
28
29 # write pdl in matlab 7.3 format.
30 matlab_write('file.dat', 'MAT73', $x);
31
32 matlab_print_info('file.dat');
33
34 =cut
35 =head1 FUNCTIONS
36
37 The functional interface.
38
39 =head2 B<matlab_read>
40
41 =head3 Usage
42
43 Return all arrays in C<$filename>
44
45 @pdls = matlab_read($filename);
46 @pdls = matlab_read($filename, {OPTIONS});
47
48 Return first array in C<$filename>
49
50 $x = matlab_read($filename);
51
52 Do not automatically convert C<1xn> and C<nx1> arrays
53 to 1-d arrays.
54
55 @pdls = matlab_read($filename, { onedr => 0 } );
56
57 Reads all data in the file C<$filename>.
58 Formats 4, 5, and 7.3 are supported. Options
59 are passed to L</B<new>>.
60
61 =cut
62 =head2 B<matlab_write>
63
64 =head3 Usage
65
66 matlab_write($filename,$x1,$x2,...);
67 matlab_write($filename,$format,$x1,$x2,...);
68
69 Automatically convert C<n> element, 1-d ndarrays to C<1xn> matlab
70 variables.
71
72 matlab_write($filename,$x1,$x2,..., {onedw => 1} );
73
74 Automatically convert to C<nx1> matlab
75 variables.
76
77 matlab_write($filename,$x1,$x2,..., {onedw => 2} );
78
79 Use zlib compression
80
81 matlab_write($filename,$x1,$x2,..., {compress => 1} );
82
83 This method writes pdls C<$x1>, C<$x2>,.... If present, C<$format>
84 must be either C<'MAT5'> or C<'MAT73'>.
85
86 =cut
87 =head2 B<matlab_print_info>
88
89 =head3 Usage
90
91 # print names and dimensions of variables.
92 matlab_print_info($filename);
93 # also print a small amount of the data.
94 matlab_print_info($filename, { data => 1 });
95 # This does the same thing.
96 matlab_print_info($filename, data => 1 );
97
98 Print information about the contents of the matlab file C<$filename>,
99 including the name, dimension and class type of the variables.
100
101 =cut
102 =head1 METHODS
103
104 =head2 B<new>
105
106 =head3 Usage
107
108 # open for writing
109 $mat = PDL::IO::Matlab->new('file.dat', '>', {format => 'MAT5'});
110
111 # default format is MAT5
112 $mat = PDL::IO::Matlab->new('file.dat', '>');
113
114 # may use 'w' or '>'
115 $mat = PDL::IO::Matlab->new('file.dat', 'w');
116
117 # supply header
118 $mat = PDL::IO::Matlab->new('file.dat', '>', { header => 'some text'} );
119
120 # read-write with rw or <>
121 $mat = PDL::IO::Matlab->new('file.dat', 'rw');
122
123 # open for reading
124 $mat = PDL::IO::Matlab->new('file.dat', '<');
125
126 =head3 Options
127
128 =over
129
130 =item format
131
132 Either C<'MAT5'> or C<'MAT73'>.
133
134 =item compress
135
136 Either C<1> for yes, or C<0> for no.
137
138 =item header
139
140 A header (a string) to write into the file.
141
142 =item namekey
143
144 A hash key that will be used to store the matlab name
145 for a variable read from a file in the header of an ndarray.
146 The default value is 'NAME'. Thus, the name can be accessed
147 via C<< $pdl->hdr->{NAME} >>.
148
149 =item varbasew
150
151 The base of the default matlab variable name that will be
152 written in the matlab file along with each ndarray. An
153 integer will be appended to the base name. This integer is
154 initialized to zero and is incremented after writing each
155 variable.
156
157 =back
158
159 The option C<compress> enables zlib compression if the zlib library
160 is available and if the data file format is C<'MAT5'>.
161
162 =cut
163 =head2 B<close>
164
165 =head3 Usage
166
167 $mat->close;
168
169 Close matlab file and free memory associated with C<$mat>.
170
171 =cut
172 =head2 B<read_next>
173
174 =head3 Usage
175
176 my $x = $mat->read_next;
177 print "End of file\n" unless ref($x);
178
179 my ($err,$x) = $mat->read_next;
180 print "End of file\n" if $err;
181
182 Read one pdl from file associated with object C<$mat>.
183
184 =cut
185 =head2 B<read_all>
186
187 =head3 Usage
188
189 my @pdls = $mat->read_all;
190
191 Read all remaining pdls from file associated with object C<$mat>.
192
193 =cut
194 =head2 B<write>
195
196 =head3 Usage
197
198 $x2->hdr->{NAME} = 'variablename';
199
200 $mat->write($x1,$x2,...);
201
202 $mat->write($x1,$x2,...,{OPTIONS});
203
204 Append pdls to open file associated with C<$mat>.
205
206 If an ndarray has a matlab name stored in the header
207 it will be used as the matlab name written to the file
208 with this ndarray. The key is in C<< $pdl->{namekey} >>,
209 with default value C<'NAME'>. If the name is not in
210 the ndarray's header, then a default value will be used.
211
212 =head3 Options
213
214 =over
215
216 =item onedw
217
218 In order to write a file that is compatible with Matlab and Octave,
219 C<onedw> must be either C<1> or C<2>. If C<onedw> is C<1> then a 1-d
220 pdl of length n is written as a as an C<nx1> pdl (a C<1xn> matlab
221 variable). If C<onedw> is C<2> then the output ndarray is C<1xn> and
222 the matlab variable C<nx1>. If C<onedw> is zero (the default), then
223 the 1-d pdl is written as a 1-d ndarray. In the last case, Octave will
224 print an error and fail to read the variable.
225
226 =item compress
227
228 If C<compress> is C<1> then zlib compression is used, if the library
229 is available and if the format is C<'MAT5'>.
230
231 =back
232
233 =cut
234 =head2 B<rewind>
235
236 =head3 Usage
237
238 $mat->rewind
239
240 Reset pointer to the head of the file.
241
242 =cut
243 =head2 B<get_filename>
244
245 =head3 Usage
246
247 $mat->get_filename
248
249 Return name of file associated with C<$mat>.
250
251 =cut
252 =head2 B<get_header>
253
254 =head3 Usage
255
256 $mat->get_header
257
258 Return the header string from the matlab data file associated with
259 C<$mat>.
260
261 =cut
262 =head2 B<get_format>
263
264 =head3 Usage
265
266 $mat->get_format
267
268 Return matlab data file format for file associated with
269 C<$mat>. One of C<'MAT4'>, C<'MAT5'>, or C<'MAT73'>.
270
271 =cut
272 =head2 B<print_all_var_info>
273
274 =head3 Usage
275
276 $mat->print_all_var_info;
277
278 # also print a small amount of data from each variable.
279 $mat->print_all_var_info( data => 1 );
280
281 Print a summary of all data in the file associated
282 with C<$mat> (starting from the next unread variable.)
283
284 =cut
285 =head1 ACCESSOR METHODS
286
287 The following are additional accessor methods for the matlab file objects
288 PDL::IO::Matlab.
289
290 get_handle set_handle get_mode set_mode get_filename set_filename get_format set_format get_varbasew set_varbasew get_onedw set_onedw get_onedr set_onedr get_namekey set_namekey get_wvarnum set_wvarnum get_compress set_compress
291 =cut
292 =head1 CAVEATS
293
294 =head2 complicating factors
295
296 There are two complicating factors when using matlab files with PDL.
297 First, matlab does not support one-dimensional vectors. Thus, a 1-d pdl
298 must be represented as either a C<1 x n> of a C<n x 1> matlab variable. Second,
299 matlab stores matrices in column-major order, while pdl stores them
300 in row-major order.
301
302 =over
303
304 =item B<one-dimensional pdls>
305
306 You can write 1-d pdls to a file with this module. This module can then read the
307 file. But, Octave will fail to read the file and print an error message.
308 See L</B<write>> for how this is handled.
309
310 =item B<column- vs. row major>
311
312 Data written by Octave (PDL) will be read by PDL (Octave) with indices transposed.
313 On the todo list is an option to physically or logically transpose the data on
314 reading and writing.
315
316 =item B<Octave requires distinct matlab variable names>
317
318 With this module, you may write more than one
319 variable, each with the same name, (the matlab name; not the
320 pdl identifier, or variable, name), to a file in MAT5
321 format. This module is then able to read all pdls from this file.
322 But, Octave, when reading this file, will overwrite all but
323 the last occurrence of the variable with the last
324 occurrence. See the method L</B<write>>.
325
326 Trying to write two pdls with the same matlab variable name in MAT73 format will cause
327 an error.
328
329 =back
330
331 =head2 other missing features, bugs
332
333 When trying to read an unsupported matlab data type from a file, this module will
334 throw an error. Supporting other data types or optionally skipping them is on
335 the todo list.
336
337 Random access of variables in a file is on the todo list. The underlying B<matio>
338 library supports this.
339
340 This module is currently built with some hardcoded data from a PDL installation, that
341 may contain platform-specific (linux) features. It may fail to
342 build or function correctly when used on other platforms.
343
344 =head1 AUTHOR
345
346 John Lapeyre, C<< <jlapeyre at cpan.org> >>
347
348 The matio library was written by Christopher C. Hulbert.
349
350 =head1 LICENSE AND COPYRIGHT
351
352 Copyright 2012 John Lapeyre.
353
354 This program is free software; you can redistribute it and/or modify it
355 under the terms of either: the GNU General Public License as published
356 by the Free Software Foundation; or the Artistic License.
357
358 See http://dev.perl.org/licenses/ for more information.
359
360 The matio library included here is
361 Copyright 2011 Christopher C. Hulbert. All rights reserved.
362 See the file matio-1.5/COPYING in the source distribution
363 of this module.
364
365 =cut