is it possible to do 'stateless' programming in matlab / how to avoid checking data integrity?

一个人想着一个人 提交于 2019-11-27 22:37:46

I like to use objects. You don't need to call clear classes on every change. It is enough to delete all instances of the "old" object.

Two very powerful additions I inherit often are handle and dynamicprops.

  1. Handle makes the object behave as handle. Very nice to come around matlabs copy-on-change behavior.
  2. Dynamic props - to do some meta programming.

About the consistency checks - why no do them when you use set.property?


Edit 1:

a simplified class that uses the database:

classdef measurement
   class
   id
properties (SetAccess = private)

end
methods
function obj = measurement(varargin)
  obj.id = varargin{1};
end

    function cs = get.class(obj)
       if isempty(obj.id)
                    cs = '';
                    return
       end
       mc = mydb.local; % use some tricks here to keep the connection open
         tmp = mym(mc,...
                    'SELECT class FROM measurements WHERE id = {Si}'...
                    ,obj.id);
         cs = tmp{1};
     end
end

Edit 2: Example for Event - Observer

classdef ObservableClass < handle

    properties 
        ListObservers=[]
        data
    end

    methods
        function obj = ObservableClass(varargin)
            obj.data = rand(100,2);
        end

        function addObserver(obj,observer)
            obj.ListObservers = [obj.ListObservers,observer];
        end

        function fireUpdate(obj)
            for i=1:numel(obj.ListObservers)
                obj.ListObservers(i).update();
            end
        end

        function set.data(obj,newData)
            obj.data = newData;
            obj.fireUpdate;
        end
    end
end

and a listener:

 classdef ObservingPlot
    properties
        fig
        observedClass
    end

    methods
        function obj = ObservingPlot(varargin)
            obj.observedClass = varargin{1};
            obj.createPlot;
            obj.observedClass.addObserver(obj);
        end

        function createPlot(obj)
            obj.fig=figure;
            plot(obj.observedClass.data);
        end

        function update(obj)
            gcf(obj.fig)
            clf
            plot(obj.observedClass.data);
        end
    end

end

The example:

a = ObservableClass()
b = ObservingPlot(a)

you can then observe when you do a: a.data=rand(100,3) - the plot will change immediatly.


Edit 3: a simple saving class

classdef SavingClass < handle

    properties 
        saveName
        data
    end

    methods
        function set.data(obj,input)
            if isempty(obj.saveName)
                obj.saveName = [tempname '.mat'];
            end
            save(obj.saveName,'input')
        end

        function out = get.data(obj)            
                out = [];
               if exist(obj.saveName,'file')                   
                   tmp = load(obj.saveName);
                   out = tmp.input;
               end
        end
    end

end

Example:

a = SavingClass;
b=rand(1000,1000);
a.data = b;

look at `whos':

Name         Size                Bytes  Class          Attributes

  a            1x1                    60  SavingClass              
  ans          1x5                    10  char                     
  b         1000x1000            8000000  double          

although you can do calculations like d = a.data-b - a takes just 60 bytes in memory - as opposed to the ~8 MB of b.


Edit 4: trick for often changing functions. When you put the logic in external commands matlab will not complain when you change the function definition there.

classdef MyOftenEditedClass < handle

    properties
        a
    end

    methods
        function set.a(obj,val)
            mySetFunctionA(obj,val)
        end

        function out=get.a(obj)
            out = myGetFunctionA(obj);
        end
    end

end
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!