MATLAB Programming
MATLAB Programming MATLAB Programming
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
- 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 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 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
- Page 548 and 549: 6 Data Import and Export The act of
- Page 550 and 551: 6 Data Import and Export Conversion
- Page 552 and 553: 6 Data Import and Export • Two sp
- Page 554 and 555: 6 Data Import and Export Exchanging
- Page 556 and 557: 6 Data Import and Export urlread('h
- Page 558 and 559: 6 Data Import and Export Once you h
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