MATLAB Object-Oriented Programming

(Joyce) #1

Save Object Data to Recreate Graphics Objects


In this section...
“What to Save” on page 13-7
“Regenerate When Loading” on page 13-7
“Change to a Stairstep Chart” on page 13-8

What to Save


Use transient properties to avoid saving what you can recreate when loading the object.
For example, an object can contain component parts that you can regenerate from data
that is saved. Regenerating these components also enables newer versions of the class to
create the components in a different way.

Regenerate When Loading


The YearlyRainfall class illustrates how to regenerate a graph when loading objects
of that class. YearlyRainfall objects contain a bar chart of the monthly rainfall for a
given location and year. The Location and Year properties are ordinary properties
whose values are saved when you save the object.

The Chart property contains the handle to the bar chart. When you save a bar chart,
MATLAB also saves the figure, axes, and Bar object and the data required to create these
graphics objects. The YearlyRainfall class design eliminates the need to save objects
that it can regenerate:


  • The Chart property is Transient so the graphics objects are not saved.

  • ChartData is a private property that provides storage for the Bar object data
    (YData).

  • The load function calls the set.ChartData method, passing it the saved bar chart
    data.

  • The setup method regenerates the bar chart and assigns the handle to the Chart
    property. Both the class constructor and the set.ChartData method call setup.


classdef YearlyRainfall < handle
properties
Location

Save Object Data to Recreate Graphics Objects
Free download pdf