Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3942416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:39:38+00:00 2026-05-20T00:39:38+00:00

I have a class with a few properties that are dependent, but that I’d

  • 0

I have a class with a few properties that are dependent, but that I’d really like to calculate only once.

I’ve just about concluded that using lazy evaluation on a dependent class property in MATLAB is either impossible or a bad idea. The original plan was to have a private logical flag for each (public) property that needs updating and to have the constructor set it to true. Then when the property accessor was called, it would check that flag and calculate the value and store it (in another private property) only if required. If the flag were false, it would simply return a copy of the cached value.

I believe the difficulty lies in a constraint on property accessors, that is, that they leave other unrelated properties alone. In other words, a get.property(self) method can’t change the state of the self object. Interestingly, this fails silently in my current class. (I.e., the neither the update flag nor the cached calculation results get set in the get. method, so the expensive calculation is run every time).

My suspicion is that changing the lazy property from a public dependent property to a method with public GetAccess but private SetAccess would work. However, I don’t like having to spoof the property convention in this way. I wish there were just a “lazy” property attribute that could do all this for me.

Am I missing something obvious? Are accessor methods for dependent class properties in MATLAB forbidden to change the class instance’s state? If so, is defining what amounts to an accessor with a private side effect the least evil way of getting the behavior I want?

Edit: here’s a test class…

classdef LazyTest
  properties(Access = public)
    % num to take factorial of
    factoriand
  end

  properties(Access = public, Dependent)
    factorial
  end

  properties(Access = private)
    % logical flag
    do_update_factorial
    % old result
    cached_factorial
  end

  methods
    function self = LazyTest(factoriand)
      self.factoriand = factoriand;
      self.do_update_factorial = true;
    end
  end

  methods
    function result = get.factorial(self)
      if self.do_update_factorial
        self.cached_factorial = factorial(self.factoriand);
        % pretend this is expensive
        pause(0.5)
        self.do_update_factorial = false
      end
      result = self.cached_factorial;
    end
  end
end

Run it with

close all; clear classes; clc

t = LazyTest(3)
t.factorial

for num = 1:10
  tic
  t.factoriand = num
  t.factorial
  toc
end

After inheriting from handle the time drops substantially.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:39:39+00:00Added an answer on May 20, 2026 at 12:39 am

    I assume you’re using a value class. With a handle class (classdef myClass < handle), which is passed by reference, you can easily modify the class from within a get-method. For example, I use what you propose in order to load data from file (if not yet loaded) or from a private, hidden property.

    Note that using a lazy dependent property the way you propose somewhat defeats the purpose of using a dependent property, i.e. the guarantee that your data is always up-to-date with the state of the properties it is derived from. Each time you change the other properties, your lazy property gets outdated.

    You could (should) add a set-method to all other properties that sets the private property to empty (isempty(obj.myPrivateProperty) is the “logical flag” you need to know whether you have to calculate). But if you do that, why not just have the set-methods call some update method that updates/recalculates all “dependent” properties right away?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few tables that I've defined like the below examples: class TableA
I have a class that offers up a few events. That class is declared
I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public
I have an IAddress class with a few properties. I then have a concrete
I have an object that has a few different List properties that contain child
I have a base class, FooBase. On it are a few standard read/write properties.
I have a class like this that I want to save in ViewState for
I have a custom class that is basically a SortedList with a few extra
I have a class that I think is too long. I don't really know
I have class method that returns a list of employees that I can iterate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.