MATLAB Examples#
This page will walk you through some simple MATLAB programs that show off DiME's API calls. All of the examples are going to operate off the assumption that the DiME server is running on the default Linux settings and that the MATLAB client has been linked in MATLAB's path. There are also ready to run examples on the DiME repository.
Join and Devices#
The simplest way to check if connections are properly being made is with the Join and Devices calls, so we'll start there.
First, you'll need to import DiME, create a few DimeClients, and add them to grops:
d1 = dime("ipc", "/tmp/dime.sock");
d2 = dime("ipc", "/tmp/dime.sock");
d3 = dime("ipc", "/tmp/dime.sock");
d1.join("turtle", "lizard");
d2.join("crocodile");
d3.join("dragon", "wyvern");
disp(d1.devices());
If you start a DiME server (dime &) and run this code, it should output:
{'crocodile'}
{'wyvern'}
{'lizard'}
{'dragon'}
{'turtle'}
There's no guarantee that the list will be in this same order, so feel free to sort it so that it's consistent. If your output is correct, then move on to the next sections. We'll expand this code block to showcase more functions.
Leave#
Leave is essentially the opposite of Join. It works almost certainly how you expect it to.
We'll expand the previous code to show what happens when DimeClients leave groups they are a part of:
d1 = dime("ipc", "/tmp/dime.sock");
d2 = dime("ipc", "/tmp/dime.sock");
d3 = dime("ipc", "/tmp/dime.sock");
d1.join("turtle", "lizard");
d2.join("crocodile", "wyvern");
d3.join("dragon", "wyvern");
d1.leave("lizard");
d2.leave("crocodile");
d3.leave("wyvern");
d1.join("lizard");
groups = d1.devices();
print(sort(groups));
Note
We're now sorting the list of groups so that the output is consistent.
The output will be:
{'dragon'}
{'lizard'}
{'turtle'}
{'wyvern'}
Leaving and rejoining works as expected: lizard is still listed because d1 rejoined before requesting the group list. Crocodile is not listed because it is empty after d2 left it. Wyvern is still listed, despite d3 leaving it, because d2 is still in it.
Wait#
From this point onward, the examples will differ substantially from those in the JavaScript and Python
pages. Basically every example from here on will have at least two instances of MATLAB running since,
unlike JavaScript and Python, the variables sent over DiME are associated with each MATLAB instance as
opposed to each DimeClient instance. Each separate instance will run a single MATLAB file. The first
instance will run testa.m, the second will run testb.m, and so on. While a single MATLAB instance
can be used to demonstrate things like Joining and Leaving, it cannot be used to show anything that
involves sending data since all variables would already be available to all DimeClient instances.
Because of this difference, we will talk about Wait first.
The Wait command forces a client to wait until another client Sends or Broadcasts a variable to it. Once the Wait is done, the client can Sync the variables that were sent to it. Note that Wait does not automatically Sync variables. Until going into more detail further down, just know that Send is for sending data to other clients and Sync is for receiving said data.
testa.m:
% testa.m
d1 = dime("ipc", "/tmp/dime.sock");
d1.join("turtle");
a = 50;
d1.send("crocodile", "a");
b = 0;
disp(b);
d1.wait();
d1.sync();
disp(b);
testb.m:
% testb.m
d2 = dime("ipc", "/tmp/dime.sock");
d2.join("crocodile");
a = 0;
disp(a);
d2.wait();
d2.sync();
disp(a);
b = 100;
d2.send("turtle", "b");
If you run these in the order of testb.m then testa.m, the output for testa.m will always be:
0
100
And the output for testb.m will always be:
0
50
Note
Without the Wait command, there would be no guarantee as to when each client will output.