Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. </p> <p>Two very powerful additions I inherit often are handle and dynamicprops. </p> <ol> <li>Handle makes the object behave as handle. Very nice to come around matlabs copy-on-change behavior. </li> <li>Dynamic props - to do some meta programming. </li> </ol> <p>About the consistency checks - why no do them when you use set.property?</p> <hr> <p>Edit 1:</p> <p>a simplified class that uses the database:</p> <pre><code>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 </code></pre> <hr> <p>Edit 2: Example for Event - Observer</p> <pre><code>classdef ObservableClass &lt; 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 </code></pre> <p>and a listener:</p> <pre><code> 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 </code></pre> <p>The example:</p> <pre><code>a = ObservableClass() b = ObservingPlot(a) </code></pre> <p>you can then observe when you do a: <code>a.data=rand(100,3)</code> - the plot will change immediatly.</p> <hr> <p>Edit 3: a simple saving class</p> <pre><code>classdef SavingClass &lt; 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 </code></pre> <p>Example:</p> <pre><code>a = SavingClass; b=rand(1000,1000); a.data = b; </code></pre> <p>look at `whos':</p> <pre><code>Name Size Bytes Class Attributes a 1x1 60 SavingClass ans 1x5 10 char b 1000x1000 8000000 double </code></pre> <p>although you can do calculations like <code>d = a.data-b</code> - <code>a</code> takes just 60 bytes in memory - as opposed to the ~8 MB of <code>b</code>.</p> <hr> <p>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.</p> <pre><code>classdef MyOftenEditedClass &lt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload