Working with Objects in Functions

Flexible Workflow

The MATLAB® language does not require you to define classes for all the code you write. You can use objects along with ordinary functions. This section illustrates the use of an object that implements the basic task of writing text to a file. Then this object is used in a function to write a text file template for a class definition.

Performing a Task with an Object

One of the advantages of defining a class instead of simply writing a function to perform a task is that classes provide better control over related data. For example, consider the task of writing data to a file. It involves the following steps:

  • Opening a file for writing and saving the file identifier

  • Using the file identifier to write data to the file

  • Using the file identifier to close the file

The Filewriter Class

This simple class definition illustrates how you might create a class to write text to a file. It shows how you can use a class definition to advantage by:

  • Hiding private data — The caller does not need to manage the file identifier.

  • Ensuring only one file identifier is in use at any time — Copies of handle objects reference the same file identifier as the original.

  • Providing automatic file closing when the object is deleted — the object's delete method takes care of cleanup without needing to be called explicitly.

This class is derived from the handle class so that a Filewriter object is a handle object. All copies of handle objects reference the same internal data so there will be only one file identifier in use, even if you make copies of the object. Also, handle classes define a delete method which is called automatically when a handle object is destroyed. This example overrides the delete method to close the file before the file identifier is lost and the file is left open.

classdef Filewriter < handle
% Property data is private to the class
   properties (SetAccess = private, GetAccess = private)
   end % properties

   % Construct an object and 
   % save the file ID  
      function obj = Filewriter(filename) 
         obj.FileID = fopen(filename,'a');
      function writeToFile(obj,text_str)
      % Delete methods are always called before a object 
      % of the class is destroyed 
      function delete(obj)
   end  % methods
end % class

Using a Filewriter Object

Note that the user provides a file name to create a Filewriter object, and then uses the class writeToFile method to write text to the file. The following statements create a file named mynewclass.m and write one line to it. The clear all command deletes the Filewriter object, which causes its delete method to be called and the file is closed.

>> fw = Filewriter('mynewclass.m');
>> fw.writeToFile('classdef mynewclass < handle')
>> clear fw
>> type mynewclass

classdef mynewclass < handle

Using Object Functionality

Filewriter objects provide functionality that you can use from functions and within other classes. You can create an ordinary function that uses this object, as the writeClassFile function does below.

This example creates only one simple class template, but another version might accept a cell array of attribute name/value pairs, method names, and so on.

function writeClassFile(classname,superclass)
% Use a Filewriter object to write text to a file 
   fw = Filewriter([classname '.m']);
   if nargin > 1  
      fw.writeToFile(['classdef ' classname ' < ' superclass])
      fw.writeToFile(['classdef ' classname])            
   fw.writeToFile('    properties ')
   fw.writeToFile(' ')
   fw.writeToFile('    end % properties')
   fw.writeToFile(' ')
   fw.writeToFile('    methods ')
   fw.writeToFile(['        function obj = ' classname '()'])
   fw.writeToFile(' ')
   fw.writeToFile('        end')
   fw.writeToFile('    end % methods')
   fw.writeToFile('end % classdef')
   delete(fw) % Delete object, which closes file

To create a class file template, call writeClassFile with the name of the new class and its superclass. Use the type command to display the contents of the file:

>> writeClassFile('myNewClass','handle')
>> type myNewClass

classdef myNewClass < handle
    end % properties
        function obj = myNewClass()
    end % methods
end % classdef

More Information on These Techniques

The Handle Superclass

Handle Class Destructor

Was this topic helpful?