User`s manual

Dynamic Data Exchange (DDE)
7-33
You set up and release advisory links with the ddeadv and ddeunadv functions.
MATLAB only supports links when MATLAB is a client.
This example establishes a DDE conversation between MATLAB, acting as a
client, and Microsoft Excel. The example extends the example in the previous
section by creating a hot link with Excel. The link updates matrix
z and
evaluates a callback when the range of cells changes. A push-button, user
interface control terminates the advisory link and the DDE conversation when
pressed. (For more information about creating a graphical user interface, see
the online MATLAB manual, Creating Graphical User Interfaces.)
% Initialize conversation with Excel.
chan = ddeinit('excel', 'Sheet1');
% Set range of cells in Excel for poking.
range = 'r1c1:r20c20';
% Create a surface of peaks plot.
h = surf(peaks(20));
% Get the z data of the surface.
z = get(h, 'zdata');
% Poke the z data to the Excel spread sheet.
rc = ddepoke(chan, range, z);
% Set up a hot link ADVISE loop with Excel
% and the MATLAB matrix 'z'.
% The callback sets the zdata and cdata for
% the surface h to be the new data sent from Excel.
rc = ddeadv(chan, range,...
'set(h,''zdata'',z);set(h,''cdata'',z);','z');
% Create a push button that will end the ADVISE link,
% terminate the DDE conversation,
% and close the figure window.
c = uicontrol('String','&Close','Position',[5 5 80 30],...
'Callback',...
'rc = ddeunadv(chan,range);ddeterm(chan);close;');