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

% yet ready.<br />

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

str = input('Enter send string (or RETURN to end): ', 's');<br />

len = length(str);<br />

if (len == 0)<br />

disp('Terminating SEND function.')<br />

break;<br />

end<br />

str = str(1:min(len, 255));<br />

% Message limited to 255 chars.<br />

end<br />

% Update the file via the memory map.<br />

m.Data(2:len+1) = str;<br />

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

% Wait until the first byte is set back to zero,<br />

% indicating that a response is available.<br />

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

pause(.25);<br />

end<br />

% Display the response.<br />

disp('response from ANSWER is:')<br />

disp(char(m.Data(2:len+1))')<br />

The answer Function<br />

The answer function starts a server that, using memory-mapping, watches<br />

for a message from send. When the message is received, answer replaces the<br />

message with an uppercase version of it, and sends this new message back<br />

to send.<br />

To use answer, call it with no inputs.<br />

function answer<br />

% Respond to SEND using memmapfile class.<br />

6-71

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

Saved successfully!

Ooh no, something went wrong!