MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 440 and 441: 6 Data Import and Export Working wi
- Page 442 and 443: 6 Data Import and Export • “Dat
- Page 444 and 445: 6 Data Import and Export file that
- Page 446 and 447: 6 Data Import and Export Function r
- Page 448 and 449: 6 Data Import and Export Importing
- Page 450 and 451: 6 Data Import and Export • rowhea
- Page 452 and 453: 6 Data Import and Export File to im
- Page 454 and 455: 6 Data Import and Export Example of
- Page 456 and 457: 6 Data Import and Export was select
- Page 458 and 459: 6 Data Import and Export Import fro
- Page 460 and 461: 6 Data Import and Export File Forma
- Page 462 and 463: 6 Data Import and Export or save ju
- Page 464 and 465: 6 Data Import and Export Data Compr
- Page 466 and 467: 6 Data Import and Export Preference
- Page 468 and 469: 6 Data Import and Export Saving fro
- Page 470 and 471: 6 Data Import and Export Loading Bi
- Page 472 and 473: 6 Data Import and Export Accessing
- Page 474 and 475: 6 Data Import and Export Sharing Me
- Page 476 and 477: 6 Data Import and Export • For sm
- Page 478 and 479: 6 Data Import and Export Note Prope
- Page 480 and 481: 6 Data Import and Export Writable:
- Page 482 and 483: 6 Data Import and Export Ifthefilet
- Page 484 and 485: 6 Data Import and Export For exampl
- Page 486 and 487: 6 Data Import and Export m = memmap
- Page 488 and 489: 6 Data Import and Export Mapping of
- Page 492 and 493: 6 Data Import and Export Note To su
- Page 494 and 495: 6 Data Import and Export d = m.Data
- Page 496 and 497: 6 Data Import and Export ans = 166x
- Page 498 and 499: 6 Data Import and Export If you hav
- Page 500 and 501: 6 Data Import and Export m.Data(60)
- Page 502 and 503: 6 Data Import and Export m = memmap
- Page 504 and 505: 6 Data Import and Export d = m.Data
- Page 506 and 507: 6 Data Import and Export Data: 1658
- Page 508 and 509: 6 Data Import and Export One MATLAB
- Page 510 and 511: 6 Data Import and Export disp('ANSW
- Page 512 and 513: 6 Data Import and Export Terminatin
- Page 514 and 515: 6 Data Import and Export character.
- Page 516 and 517: 6 Data Import and Export Table6-2 A
- Page 518 and 519: 6 Data Import and Export A = dlmrea
- Page 520 and 521: 6 Data Import and Export number of
- Page 522 and 523: 6 Data Import and Export Exporting
- Page 524 and 525: 6 Data Import and Export A = [ 1 2
- Page 526 and 527: 6 Data Import and Export diary off
- Page 528 and 529: 6 Data Import and Export Importing
- Page 530 and 531: 6 Data Import and Export Working wi
- Page 532 and 533: 6 Data Import and Export Exporting
- Page 534 and 535: 6 Data Import and Export 3 Close th
- Page 536 and 537: 6 Data Import and Export type = Mic
- Page 538 and 539: 6 Data Import and Export ndata = xl
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