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 disp('ANSWER server is awaiting message'); filename = fullfile(tempdir, 'talk_answer.dat'); % Create the communications file if it is not already there. if ~exist(filename, 'file') [f, msg] = fopen(filename, 'wb'); if f ~= -1 fwrite(f, zeros(1,256), 'uint8'); fclose(f); else error('MATLAB:demo:answer:cannotOpenFile', ... 'Cannot open file "%s": %s.', filename, msg); end end % Memory map the file. m = memmapfile(filename, 'Writable', true, 'Format', 'uint8'); while true % Wait till first byte is not zero. while m.Data(1) == 0 pause(.25); end end % The first byte now contains the length of the message. % Get it from m. msg = char(m.Data(2:1+m.Data(1)))'; % Display the message. disp('Received message from SEND:') disp(msg) % Transform the message to all uppercase. m.Data(2:1+m.Data(1)) = upper(msg); % Signal to SEND that the response is ready. m.Data(1) = 0; 6-72

Accessing Files with Memory-Mapping Running the Demo Here is what the demonstration looks like when it is run. First, start two separate MATLAB sessions on the same computer system. Call the send function in one and the answer function in the other to create a map in each of the processes’ memory to the common file: % Run SEND in the first MATLAB session. send Enter send string (or RETURN to end): % Run ANSWER in the second MATLAB session. answer ANSWER server is awaiting message Next, enter a message at the prompt displayed by the send function. MATLAB writes the message to the shared file. The second MATLAB session, running the answer function, loops on byte 1 of the shared file and, when the byte is written by send, answer readsthemessagefromthefileviaitsmemorymap. The answer function then puts the message into uppercase and writes it back to the file, and send (waiting for a reply) reads the message and displays it: % SEND writes a message and reads the uppercase reply. Hello. Is there anybody out there? response from ANSWER is: HELLO. IS THERE ANYBODY OUT THERE? Enter send string (or RETURN to end): % ANSWER reads the message from SEND. Received message from SEND: Hello. Is there anybody out there? send writes a second message to the file. answer readsit,putitinto uppercase, and then writes the message to the file: % SEND writes a second message to the shared file. I received your reply. response from ANSWER is: I RECEIVED YOUR REPLY. Enter send string (or RETURN to end): 6-73

6 Data Import and Export<br />

disp('ANSWER server is awaiting message');<br />

filename = fullfile(tempdir, 'talk_answer.dat');<br />

% Create the communications file if it is not already there.<br />

if ~exist(filename, 'file')<br />

[f, msg] = fopen(filename, 'wb');<br />

if f ~= -1<br />

fwrite(f, zeros(1,256), 'uint8');<br />

fclose(f);<br />

else<br />

error('<strong>MATLAB</strong>:demo:answer:cannotOpenFile', ...<br />

'Cannot open file "%s": %s.', filename, msg);<br />

end<br />

end<br />

% Memory map the file.<br />

m = memmapfile(filename, 'Writable', true, 'Format', 'uint8');<br />

while true<br />

% Wait till first byte is not zero.<br />

while m.Data(1) == 0<br />

pause(.25);<br />

end<br />

end<br />

% The first byte now contains the length of the message.<br />

% Get it from m.<br />

msg = char(m.Data(2:1+m.Data(1)))';<br />

% Display the message.<br />

disp('Received message from SEND:')<br />

disp(msg)<br />

% Transform the message to all uppercase.<br />

m.Data(2:1+m.Data(1)) = upper(msg);<br />

% Signal to SEND that the response is ready.<br />

m.Data(1) = 0;<br />

6-72

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

Saved successfully!

Ooh no, something went wrong!