MATLAB Programming
MATLAB Programming MATLAB Programming
6 Data Import and Export If you haven’t done so already, generate a test data file for use in the following examples by executing the gendatafile function defined under “Constructing a memmapfile Object” on page 6-40: gendatafile('records.dat', 5000); Now call the memmapfile constructor to create the object: m = memmapfile('records.dat', ... 'Format', { ... 'uint16' [5 8] 'x'; ... 'double' [4 5] 'y' }); If you are going to modify the mapped file, be sure that you have write permission, and that you set the Writable property of the memmapfile object to true (logical 1): m.Writable = true; (You do not have to set Writable as a separate command, as done here. You can include a Writable parameter-value argument in the call to the memmapfile constructor.) Read from the 5-by-8 matrix x at m.Data(2): d = m.Data; d(2).x ans = 35330 4902 31861 16877 23791 61500 52748 16841 51314 58795 16860 43523 8957 5182 16864 60110 18415 16871 59373 61001 52007 16875 26374 28570 16783 4356 52847 53977 16858 38427 16067 33318 65372 48883 53612 16861 18882 39824 61529 16869 Update all values in that matrix using a standard MATLAB assignment statement: d(2).x = d(2).x * 1.5; Verify the results: 6-60
Accessing Files with Memory-Mapping d(2).x ans = 52995 7353 47792 25316 35687 65535 65535 25262 65535 65535 25290 65285 13436 7773 25296 65535 27623 25307 65535 65535 65535 25313 39561 42855 25175 6534 65535 65535 25287 57641 24101 49977 65535 65535 65535 25292 28323 59736 65535 25304 This section covers the following topics: • “Dimensions of the Data Field” on page 6-61 • “Writing Matrices to a Mapped File” on page 6-62 • “Selecting Appropriate Data Types” on page 6-65 • “Working with Copies of the Mapped Data” on page 6-65 • “Invalid Syntax for Writing to Mapped Memory” on page 6-66 Dimensions of the Data Field The dimensions of a memmapfile object’s Data fieldaresetatthetimeyou construct the object and cannot be changed. This differs from other MATLAB arrays that have dimensions you can modify using subscripted assignment. For example, you can add a new column to the field of a MATLAB structure: A.s = ones(4,5); A.s(:,6) = [1 2 3 4]; size(A.s) ans = 4 6 % Add new column to A.s But not to a similar field of a structure that represents data mapped from a file. The following assignment to m.Data(60).y does not expand the size of y, but instead generates an error: m.Data(60) ans = x: [5x8 uint16] y: [4x5 double] 6-61
- 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 490 and 491: 6 Data Import and Export Repeating
- 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 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
- Page 540 and 541: 6 Data Import and Export 41 42 43 4
- Page 542 and 543: 6 Data Import and Export Note While
- Page 544 and 545: 6 Data Import and Export Use these
- Page 546 and 547: 6 Data Import and Export Writing Bi
Accessing Files with Memory-Mapping<br />
d(2).x<br />
ans =<br />
52995 7353 47792 25316 35687 65535 65535 25262<br />
65535 65535 25290 65285 13436 7773 25296 65535<br />
27623 25307 65535 65535 65535 25313 39561 42855<br />
25175 6534 65535 65535 25287 57641 24101 49977<br />
65535 65535 65535 25292 28323 59736 65535 25304<br />
This section covers the following topics:<br />
• “Dimensions of the Data Field” on page 6-61<br />
• “Writing Matrices to a Mapped File” on page 6-62<br />
• “Selecting Appropriate Data Types” on page 6-65<br />
• “Working with Copies of the Mapped Data” on page 6-65<br />
• “Invalid Syntax for Writing to Mapped Memory” on page 6-66<br />
Dimensions of the Data Field<br />
The dimensions of a memmapfile object’s Data fieldaresetatthetimeyou<br />
construct the object and cannot be changed. This differs from other <strong>MATLAB</strong><br />
arrays that have dimensions you can modify using subscripted assignment.<br />
For example, you can add a new column to the field of a <strong>MATLAB</strong> structure:<br />
A.s = ones(4,5);<br />
A.s(:,6) = [1 2 3 4];<br />
size(A.s)<br />
ans =<br />
4 6<br />
% Add new column to A.s<br />
But not to a similar field of a structure that represents data mapped from a<br />
file. The following assignment to m.Data(60).y does not expand the size<br />
of y, but instead generates an error:<br />
m.Data(60)<br />
ans =<br />
x: [5x8 uint16]<br />
y: [4x5 double]<br />
6-61