Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a direct way to create a static property in Matlab. The only difference between this implementation and a hypothetical (but impossible; see Mikhail's answer) true static property is the syntax for setting the member variable.</p> <pre><code>classdef StaticVarClass methods (Static = true) function val = staticVar(newval) persistent currentval; if nargin &gt;= 1 currentval = newval; end val = currentval; end end end </code></pre> <p>Now the static property staticVar can be read via:</p> <pre><code>StaticVarClass.staticVar </code></pre> <p>...and be set via:</p> <pre><code>StaticVarClass.staticVar(newval); </code></pre> <p>So, for instance, this is the expected output from a test of this functionality:</p> <pre><code>&gt;&gt; StaticVarClass.staticVar ans = [] &gt;&gt; StaticVarClass.staticVar('foobar') ans = foobar &gt;&gt; StaticVarClass.staticVar ans = foobar &gt;&gt; </code></pre> <p>This approach works just as well for private static properties like you requested, but the demo code is a little longer. Note that this is not a handle class (though it would work perfectly well on a handle class as well).</p> <pre><code>classdef StaticVarClass methods (Access = private, Static = true) function val = staticVar(newval) persistent currentval; if nargin &gt;= 1 currentval = newval; end val = currentval; end end methods function this = setStatic(this, newval) StaticVarClass.staticVar(newval); end function v = getStatic(this) v = StaticVarClass.staticVar; end end end </code></pre> <p>...and the test:</p> <pre><code>&gt;&gt; x = StaticVarClass x = StaticVarClass with no properties. Methods &gt;&gt; x.getStatic ans = [] &gt;&gt; x.setStatic('foobar') ans = StaticVarClass with no properties. Methods &gt;&gt; x.getStatic ans = foobar &gt;&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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.
    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