23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Accessing Files with Memory-Mapping<br />

• If you specify a repeat value other than Inf, and that value would cause the<br />

map to extend beyond the end of the file, then <strong>MATLAB</strong> generates an error.<br />

Considering the last example, if the part of the file from m.Offset to the end<br />

were 70 bytes (instead of the 72 bytes required to repeat m.Format three<br />

times) and you used a Repeat value of Inf, then only two full repetitions of<br />

the specified format would have been mapped. The end result would be as if<br />

you had constructed the map with this command:<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', 2);<br />

If Repeat were set to 3 and you had only 70 bytes to the end of the file, you<br />

would get an error.<br />

Note memmapfile does not expand or append to a mapped file. Use standard<br />

file I/O functions like fopen and fwrite to do this.<br />

Setting the Type of Access<br />

You can map a file region to allow either read-only or read and write access<br />

to its contents. Pass a Writable parameter and value in the memmapfile<br />

constructor, or set m.Writable on an existing object to set the type of access<br />

allowed:<br />

objname = memmapfile(filename, 'Writable', trueorfalse)<br />

The value passed can be either true (equal to logical(1)) orfalse (equal<br />

to logical(0)). By default, it is false, meaning that the mapped region<br />

is read only.<br />

To map a read and write region of the file records.dat in memory, type<br />

m = memmapfile('records.dat', 'Writable', true);<br />

6-53

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

Saved successfully!

Ooh no, something went wrong!