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 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

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

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

Saved successfully!

Ooh no, something went wrong!