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.
One of the advantages of an object instead of writing a function to perform a task is that objects can encapsulate 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
This class writes text to a file. The advantage of using a class for this purpose is to:
Hide private data — The caller does not need to manage the file identifier.
Ensure only one file identifier is in use at any time — Copies of handle objects reference the same file identifier as the original.
Provide automatic file closing when the object is
deleted — the object's
delete method takes
care of cleanup without needing to be called explicitly.
Filewriter class derives from the
handle class. Therefore,
Filewriter object is a handle object. All copies
of handle objects reference the same internal data. There is only
one file identifier in use, even if you make copies of the object.
Also, handle classes define a
which is called automatically when MATLAB destroys a handle object.
This example overrides the
delete method. This
method closes the file before deleting the object and losing file
classdef Filewriter < handle properties (Access = private) FileID end methods function obj = Filewriter(filename) obj.FileID = fopen(filename,'a'); end function writeToFile(obj,text_str) fprintf(obj.FileID,'%s\n',text_str); end function delete(obj) fclose(obj.FileID); end end end
classdef Filewriter < handle
This class derives from
For general information on class destructors, see Handle Class Destructor
properties (Access = private) FileID end
For method syntax, see Methods and Functions
function obj = Filewriter(filename) if nargin < 1 error('You must specify a filename') else obj.FileID = fopen(filename,'a'); end end
function writeToFile(obj,text_str) fprintf(obj.FileID,'%s\n',text_str); end
function delete(obj) fclose(obj.FileID); end
Provides a file name to create a
writeToFile method to write text to the
file. The following statements create a file named
write one line to it. The
clear all command deletes
Filewriter object. Clearing the object variable
delete method, which closes the file.
fw = Filewriter('MyNewClass.m'); writeToFile(fw,'classdef < handle') clear fw type MyNewClass
classdef MyNewClass < handle
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
This example creates only one simple class template, but another version could accept a cell array of attribute name/value pairs, method names, and so on.
function writeClassFile(classname,superclass) fw = Filewriter([classname '.m']); if nargin > 1 writeToFile(fw,['classdef ' classname ' < ' superclass]) else writeToFile(fw,['classdef ' classname]) end writeToFile(fw,' properties ') writeToFile(fw,' ') writeToFile(fw,' end') writeToFile(fw,' ') writeToFile(fw,' methods ') writeToFile(fw,' function') writeToFile(fw,' ') writeToFile(fw,' end') writeToFile(fw,' end') writeToFile(fw,'end') end
To create a class file template, call
the name of the new class and its superclass. Use the
to display the contents of the file:
writeClassFile('MyNewClass','handle') edit MyNewClass
Here is the template file in the editor ready to add code: