Day 22: Make your data pop with this dynamic update function

Jozsef Meszaros
3 min readMay 23, 2020

If you want to make plots with displays that you can manipulate, this is the post for you. However, unless you already have experience using dynamic cursor mode, you will want to first jump over to my posts from Day 10 and Day 11.

It’s great to show the averages of your time series data to compare groups, but sometimes you also want to see the spread of the underlying data. Using a dynamic update function will help you easily accomplish this, and it’ll look great too.

We’ll make some slightly complex data, based on a sigmoid function. Let’s imagine this data coming into existence.

You have ten trials during which a test subject has an opportunity to transition from one area to another area — you provide a stimulus to cue this transition. The ideal trial looks something like this:

We can create this data with some noise quickly using arrayfun and some math.

rng(4)
grp1_data = arrayfun( @(x) (1./(1+exp(x-linspace(-10,10,20))) + normrnd(0,.1,1,20))', linspace(-3,4,10), 'UniformOutput', false );
grp1_data = cell2mat( grp1_data );

grp2_data = arrayfun( @(x) 2*(1./(1+exp(x-linspace(-10,10,20))) + normrnd(0,.1,1,20))', linspace(-1,1,10), 'UniformOutput', false );
grp2_data = cell2mat( grp2_data );

I’ve transposed it a couple of times to make sure that each column corresponds to a trial. Each row is a time point. Now if you plot the data, you should see something like this:

If you displayed only the means:

With a dynamic plot, you can do both with one figure.

As with any dynamic plot in MATLAB, there are two steps to take.

  1. Create the dynamic function as a separate helper file and make sure you save it in a working directory (e.g., a directory that MATLAB looks for functions). You can tell by saving this code as myupdatefcn_disp3.m and then typing “which myupdatefcn_disp3”. It should return a location.

2. Now plot the easy part. Plot the figure with the correct tags.

Run this script, and you should be able to click on either the line for the group 1 mean or the group 2 mean, and the underlying data will pop out for you!

--

--