5.0

5.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 5.51 KB File ID: #17240

DynamicPlot: An example of handle semantics with MATLAB classes

by Gautam Vallabha

 

30 Oct 2007 (Updated 31 Oct 2007)

A MATLAB class that implements an automatically-updated display with pause and resume.

| Watch this File

File Information
Description

obj = dynamicplot(genFcn, T)
creates a figure that is automatically updated (by calling a generator function genFcn) every T seconds. obj is a handle to the created object.
 
E.g.: d = dynamicplot(@() randn(1,100), 0.5);

-------------------------------------
 
  The DYNAMICPLOT class demonstrates how to get reference (aka. handle) semantics using MATLAB classes. Typically when one modifies a MATLAB object, it needs be returned to the caller:
  obj = set(obj, 'myParameter', 10.0);
But with handle semantics, you can simply do this:
   set(obj, 'myParameter', 10.0);

Handle semantics is especially useful with objects that need to be updated autonomously (e.g., acquiring data from a data aquisition device or an instrument).
 
The handle semantics were accomplished as follows.
  (1) the constructor defines the instance variables and
  (2) returns a set of function handles to access these instance variables.
  (3) Other methods use only the function handles to access and modify the data.
  
Detailed example of handle semantics:
 
  d = dynamicplot(@() randn(1,100), 0.5);
  getdata(d) % try this several times
  setUpdatePeriod(d, 0.2)
  getNumUpdates(d) % try several times
  setLineStyle(d,'color','r');
 
  % Object assignment respects the
  % handle semantics. d and d2 refer
  % to the same object.
  d2 = d;
  pauseDisplay(d);
  resumeDisplay(d2);

  % Validity checks can be built into
  % the object itself
  delete(d);
  getdata(d) % will generate an error
  getdata(d2) % generates error also

  % It is possible to have multiple
  % objects without any clashes

  d1 = dynamicplot(@() randn(1,100), 0.1);
  d2 = dynamicplot(@() rand(1,100), 0.1);
  pauseDisplay(d1);
  getNumUpdates(d1)
  getNumUpdates(d2)
  resumeDisplay(d1);
  delete(d1);

--------------------------------------

NOTE: The primary purpose of the DYNAMICPLOT class is to demonstrate how to get handle semantics with MATLAB classes. Feel free to improve or extend the functionality.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
01 Nov 2007 Yi Cao

A nice tool and a clever idea to explore what MATLAB can do.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
dynamic Gautam Vallabha 22 Oct 2008 14:39:06
handle class Gautam Vallabha 22 Oct 2008 14:39:06
automatic refresh Gautam Vallabha 22 Oct 2008 14:39:06
plotting Gautam Vallabha 22 Oct 2008 14:39:06

Contact us at files@mathworks.com