MATLAB window example

Hi Ralf,

I’ve attached an example MWorks experiment and MATLAB function that demonstrate how to use MWClient’s MATLAB window. First, I’ll explain how to load and run the experiment, and then I’ll explain how things work.

To run the example, follow these steps:

  1. If you haven’t already done so, follow the steps described in Setup for using MATLAB.

  2. Open MWClient and connect to MWServer.

  3. Load the attached example experiment (matlab_window_demo.xml).

  4. Open the MATLAB window: Click on the magnifying glass icon in the lower right of the main MWClient window, then select “MATLAB Interface Window” from the list of options.

  5. In the MATLAB window’s “Synchronization Variable” field, enter “sync”. (Be sure to hit Return after typing “sync”; otherwise, the setting won’t take.)

  6. Click “Select .m file” and select the attached MATLAB file (matlab_window_demo.m).

  7. Make sure that “Process 0 events” and “Collecting Data” are both checked.

  8. Click the “Events” button. In the variable-selection window that pops up, make sure that “rand_var” and “sync” are checked.

  9. Run the experiment. MATLAB should launch and display a histogram containing the values of “rand_var”. The MATLAB function is written to accumulate values from all runs, so if you run the experiment again, the new values will be added to the histogram. The function reports how many values are in the histogram, so if you run the experiment three times, you should see the following under “Matlab output”:

     Drawing histogram (10 values)
     Drawing histogram (20 values)
     Drawing histogram (30 values)
    

Now that you’ve seen the MATLAB window in action, here’s how it works:

The basic function of the MATLAB window is to allow you to collect the values of variables of interest and send them to a MATLAB function for processing. You select the variables you want to monitor by clicking the “Events” button and checking the names of the variables. (In the attached example, we monitor only one variable, “rand_var”, but you can monitor as many variables as you like.)

The synchronization variable allows you to control when variable values are collected. (It’s typically called “sync”, but you can call it whatever you want.) In the experiment, sync is initially set to 0. When you want to start collecting values, you set sync to 1. From that point on, the MATLAB window will record any changes to the variables you’re monitoring. When you’re finished collecting values and want to send the data to your MATLAB function for processing, set sync to 0.

The MATLAB function takes two arguments. The first (called “data” in the example) is a structure that contains both the event codec (which maps variable names to MWorks event codes) and the list of collected events. Every time a variable changes value, an MWorks event is generated, which contains both the time of the change and the new value. The event list passed to your MATLAB function contains all the variable-change events for your monitored variables. The example script demonstrates how to use the codec and event list to extract the values for the variables you’re interested in.

The second argument to your MATLAB function (called “values” in the example) provides a way to pass data from one invocation of the function to the next. The first time your function is invoked, this parameter is omitted. When the function returns, the return value is stored. The next time the function is invoked, the stored return value is passed back to the function as its second argument. The attached example uses this mechanism to pass the cumulative histogram data from one invocation to the next.

Hopefully this isn’t too confusing. It’s simpler than it sounds, and once you’ve tried it a few times, I think you’ll find that it’s pretty easy to use. If you have any questions or need more info, please let me know!

Chris

Attachments:

For the benefit of users who may come across this discussion in the future: There is now a knowledge base article that explains how to use the MATLAB window.