The class definition in MATLAB classdef

Source: Internet
Author: User
Tags modulus

matlab can also write object-oriented code, the first manifestation of the class can be defined, and can inherit, There are many benefits to using classes (class), and one of the important benefits is resolving variable name collisions and making the function, object structure clear. The static function of class can invoke a class's member function directly in an instance that does not define a class, such as defining

ClassDef Tools < Handle    methods (Static = True)        function A = Test (b, c)            a = B + C;        End    EndEnd

you can then go directly through

A = Tools.test (b, c);
Call the function.

Classdel is the definition of the class name, the following < is to represent inheritance, MATLAB allows multiple inheritance, inherits from the handle class, the handle class defines a lot of object handling functions, For example, AddListener and notify and delete, such as the function of the object, is an abstract class, can not be instantiated, in particular, see the reference "2."

The method of the class is placed in the methods ... end.


The following is a slightly more complex definition of a class

ClassDef tensiledata Properties Material = ' carbon steel ';      Samplenumber = 0; Stress Strain End Properties (Dependent) Modulus End methods Function td = Tensiledata (material, Samplenum,stress,strain) If Nargin > 0 TD.            Material = Material; Td.            Samplenumber = Samplenum; Td.            Stress = Stress; Td.         Strain = strain; End end% Tensiledata End methods Function obj = set. Material (obj,material) if ~ (Strcmpi (Material, ' aluminum ') | | ... strcmpi (material, ' stainless steel ') | | ... strcmpi (material, ' carbon Steel ')) error (' Material must be aluminum, stainless steel, or carbo n Steel ') end obj.      Material = Material; End% Material Set function function modulus = get. Modulus (obj) ind = find (obj. Strain > 0); % Find nonzero strain modulus = mean (obj. Stress (Ind)./obj.      Strain (Ind)); End% Modulus get function function obj = set. Modulus (obj,~) fprintf ('%s%d\n ', ' modulus is: ', obj.      Modulus) error (' You cannot set modulus explicitly '); End Function disp (TD) Fprintf (1, ' Material:%s\nsample number:%g\nmodulus:%1.5g\n ',... td.m Aterial,td. Samplenumber,td.      modulus); End% disp function plot (Td,varargin) plot (TD. Strain,td. stress,varargin{:}) title ([' Stress/strain Plot for Sample ', NUM2STR (TD). Samplenumber)] xlabel (' Strain% ') ylabel (' Stress (PSI) ') End% plot end methods (Access = ' PR Ivate ')% Access by class Members only function M = calcmodulus (TD)% over-simplified calculation of Elastic Modulus ind = FIND (TD. Strain > 0); % Find nonzero strain M = mean (TD. Stress (Ind)./td.      Strain (Ind)); End% Calcmodulus EndEnd% classdef

In the above code,

ClassDef Tensiledata

...

End

is to define a Tensiledata class. Code:

 properties
      Material = ' Carbon steel ';
      samplenumber = 0;
      stress
      strain
   end
  is a property that defines this class, which is a member variable of a class in C + +. However, unlike C + +, the class definition in MATLAB has a more special place, that is, to define the dependency property, such as the following code:

Properties (Dependent)
Modulus
End

This means that the Modulus property is a dependency property, and its value is computed from other properties, where the default property value of dependent is true.

Its value is implemented by the following function:

% modulus Get function

function modulus = get. Modulus (obj)
IND = Find (obj. Strain > 0); % Find Nonzero strain
modulus = mean (obj. Stress (Ind)./obj. Strain (Ind));
End

The definition of a method (function) of a class occurs in the form of methods. End. As defined by the following class method:

Methods
function td = Tensiledata (Material,samplenum,stress,strain)
If Nargin > 0
Td. Material = Material;
Td. Samplenumber = Samplenum;
Td. Stress = Stress;
Td. Strain = strain;
End
End
End

The function block defines the Tensiledata constructor method. The last method in the preceding code is methods (Access = ' private ')

Access = ' private ' means that the method can only be accessed and modified by the class itself and is a private member method. Where properties access can also be divided into

Setaccess and Getaccess, the property values are the same as access.

The use of object-oriented will inevitably lead to a higher cost of the program, and the related discussion can look at "1":

The encapsulated function can be called multiple times for a very long time.

So in the end it should not be encapsulated, but also depends on the actual situation, if the function itself is particularly simple, and will be called by the loop, preferably through the form of m file functions. Previously, MIT Daniel gave more advice

  1. Although the speed of for-loop has improved significantly, vectorization (vectorization) is still an important way to improve efficiency, especially when it is possible to rewrite the operation into matrix multiplication.
  2. In many cases, the for-loop itself does not pose much of a problem, especially when the loop body itself requires more computation. At this time, the key to improving the probability is to improve the circulation body itself rather than remove the for-loop.
  3. MATLAB's function call procedure (non-built-in functions) has a significant overhead, so in the high efficiency of the Code, should use the flat call structure as far as possible, in the case of keeping the code clear and maintainable, as far as possible to write expressions and use built-in Functions to avoid unnecessary custom function call procedures. In a lot of cycles (including functions that actually contain loops, such as cellfun, Arrayfun, etc.), a long call chain is generated, which can have a significant overhead.
  4. When the function is called, the built-in function is preferred, then the normal m-file functions, and then the function handle or anonymous. When using function handle or anonymous function as a parameter, if the function is called more than once, it is best to catch it with a variable before passing it. In this way, the repeated parsing process can be avoided effectively.
  5. Where possible, using the numeric array or struct array, they are significantly more efficient than the cell array (dozens of times times or more). For structs, use the normal domain (field) access method as much as possible, in the case of non-efficiency-critical, less-executed, and more flexible code that can consider using dynamic names for domain access.
  6. Although object-oriented from the point of view of software engineering more advantageous, and the use of object is very convenient, but Matlab at present, the implementation of OO is very inefficient, in the efficiency of the key code should be cautious with objects.
  7. If you need to design a class, you should use the normal property as much as possible, and avoid the flexible but inefficient dependent property. Avoid overloading the subsref and SUBSASGN functions if it is not really necessary, as this will take over the interface calls to object, often with very large overhead (tens of thousands of times slower), even making code that is hardly a problem a performance bottleneck.


Reference documents:

"1" http://zhiqiang.org/blog/it/class-wrapper-functions-in-matlab.html

"2" Http://cn.mathworks.com/help/matlab/ref/handle.html?searchHighlight=handle

"3" http://www.cnblogs.com/magic-cube/archive/2011/11/08/2241580.html

"4" http://anony3721.blog.163.com/blog/static/5119742011911111232557/

The class definition in MATLAB classdef

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.