| MATLAB® | ![]() |
| On this page… |
|---|
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.
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
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) FileID end % properties methods % Construct an object and % save the file ID function obj = Filewriter(filename) obj.FileID = fopen(filename,'a'); end function writeToFile(obj,text_str) fprintf(obj.FileID,'%s\n',text_str); end % Delete methods are always called before a object % of the class is distroyed function delete(obj) fclose(obj.FileID); end end % methods end % class
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
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]) else fw.writeToFile(['classdef ' classname]) end 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 end
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 properties end % properties methods function obj = myNewClass() end end % methods end % classdef
![]() | Developing Classes — Typical Workflow | Using the Editor and Debugger with Classes | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |