MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

6 Data Import and Export Repeating a Format Scheme Once you have set a Format value for the memmapfile object, you can have MATLAB apply that format to the file data multiple times by specifying a Repeat value when you call the memmapfile constructor: objname = memmapfile(filename, ... 'Format', formatspec, ... 'Repeat', count) The Repeat value applies to the whole format specifier, whether that specifier describes just a single data type that repeats, or a more complex format that includes various data types and array shapes. The default Repeat value is infinity (inf), which means that the full extent of the Format specifier repeats as many times as possible within the mapped region. The next example maps a file region identical to that of the previous example, except the pattern of int16, uint32, andsingle data types is repeated only three times within the mapped region of the file: m = memmapfile('records.dat', ... 'Offset', 2048, ... 'Format', { ... 'int16' [2 2] 'model'; ... 'uint32' [1 1] 'serialno'; ... 'single' [1 3] 'expenses'}, ... 'Repeat', 3); You can change the value of the Repeat property at any time. To change the repeat value to 5, type m.Repeat = 5; Property names, like Repeat, arenotcasesensitive. Keeping the Repeated Format Within the Mapped Region. MATLAB maps only the full pattern specified by the Format property. If you repeat a format such that it would cause the map to extend beyond the end of the file, then either of two things can happen: • If you specify a repeat value of Inf, then only those repeated segments that fit within the file in their entirety are applied to the map. 6-52

Accessing Files with Memory-Mapping • If you specify a repeat value other than Inf, and that value would cause the map to extend beyond the end of the file, then MATLAB generates an error. Considering the last example, if the part of the file from m.Offset to the end were 70 bytes (instead of the 72 bytes required to repeat m.Format three times) and you used a Repeat value of Inf, then only two full repetitions of the specified format would have been mapped. The end result would be as if you had constructed the map with this command: m = memmapfile('records.dat', ... 'Offset', 2048, ... 'Format', { ... 'int16' [2 2] 'model'; ... 'uint32' [1 1] 'serialno'; ... 'single' [1 3] 'expenses'}, ... 'Repeat', 2); If Repeat were set to 3 and you had only 70 bytes to the end of the file, you would get an error. Note memmapfile does not expand or append to a mapped file. Use standard file I/O functions like fopen and fwrite to do this. Setting the Type of Access You can map a file region to allow either read-only or read and write access to its contents. Pass a Writable parameter and value in the memmapfile constructor, or set m.Writable on an existing object to set the type of access allowed: objname = memmapfile(filename, 'Writable', trueorfalse) The value passed can be either true (equal to logical(1)) orfalse (equal to logical(0)). By default, it is false, meaning that the mapped region is read only. To map a read and write region of the file records.dat in memory, type m = memmapfile('records.dat', 'Writable', true); 6-53

6 Data Import and Export<br />

Repeating a Format Scheme<br />

Once you have set a Format value for the memmapfile object, you can have<br />

<strong>MATLAB</strong> apply that format to the file data multiple times by specifying a<br />

Repeat value when you call the memmapfile constructor:<br />

objname = memmapfile(filename, ...<br />

'Format', formatspec, ...<br />

'Repeat', count)<br />

The Repeat value applies to the whole format specifier, whether that specifier<br />

describes just a single data type that repeats, or a more complex format that<br />

includes various data types and array shapes. The default Repeat value is<br />

infinity (inf), which means that the full extent of the Format specifier repeats<br />

as many times as possible within the mapped region.<br />

The next example maps a file region identical to that of the previous example,<br />

except the pattern of int16, uint32, andsingle data types is repeated only<br />

three times within the mapped region of the file:<br />

m = memmapfile('records.dat', ...<br />

'Offset', 2048, ...<br />

'Format', { ...<br />

'int16' [2 2] 'model'; ...<br />

'uint32' [1 1] 'serialno'; ...<br />

'single' [1 3] 'expenses'}, ...<br />

'Repeat', 3);<br />

You can change the value of the Repeat property at any time. To change<br />

the repeat value to 5, type<br />

m.Repeat = 5;<br />

Property names, like Repeat, arenotcasesensitive.<br />

Keeping the Repeated Format Within the Mapped Region. <strong>MATLAB</strong><br />

maps only the full pattern specified by the Format property. If you repeat a<br />

format such that it would cause the map to extend beyond the end of the file,<br />

then either of two things can happen:<br />

• If you specify a repeat value of Inf, then only those repeated segments that<br />

fit within the file in their entirety are applied to the map.<br />

6-52

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!