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

  • SEARCH
  • Home
  • 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 6185337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:41:32+00:00 2026-05-24T01:41:32+00:00

I’m trying to implement a class with a property that can be possibly provided

  • 0

I’m trying to implement a class with a property that can be possibly provided to the constructor or possibly generated in some other method. I don’t want the data saved to disk or generated on load. What I have so far is:

classdef MyClass
        properties(GetAccess = public, SetAccess = private)
            Property1
            Property2
            Property3
        end
        properties(Access = private)
            Property4
        end
        properties(Transient = true)
            ProblemProperty
        end
        properties(Dependent = true, Transient = true)
            Property5
        end

        methods
            function MyClass
                % Constructor.
            end

            function val = get.Property5(B)
                val = SomeFunction(Property1);
            end

            function val = get.ProblemProperty(B)
                if isempty(B.ProblemProperty)
                    B = GenerateProblemProperty(B);
                end
                val = B.ProblemProperty;
            end

            function B = GenerateProblemProperty(B)
                B.ProblemProperty = AnotherFunction(B.Property2);
            end
        end
    end

The problem is that when I try to save the object to disk, Matlab calls the get.ProblemProperty method (confirmed by running the profiler on just the save statement). The ProblemProperty field is empty and I want it to stay that way. It doesn’t call the get.Property5 method.

How do I avoid the call to get.ProblemProperty?

  • 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-24T01:41:34+00:00Added an answer on May 24, 2026 at 1:41 am

    Since the property can sometimes be set (i.e. in the constructor) then this property is not strictly dependent. One solution is to store the settable value in a private property (CustomProblemProperty in the example below) in the constructor. The get method of ProblemProperty would then check return this private property value if it’s not empty and otherwise return the generated value.

    classdef MyClass
        properties(GetAccess = public, SetAccess = private)
            Property1
            Property2
            Property3
        end
        properties(Access = private)
            Property4
            CustomProblemProperty
        end
        properties(Dependent = true, Transient = true)
            ProblemProperty
            Property5
        end
    
        methods
            function B = MyClass(varargin)
                if nargin == 1
                   B.CustomProblemProperty = varargin{1};
                end
            end
    
            function val = get.Property5(B)
                val = SomeFunction(Property1);
            end
    
            function val = get.ProblemProperty(B)
                if isempty(B.CustomProblemProperty)
                   val = AnotherFunction(B.Property2);
                else
                   val = B.CustomProblemProperty;
                end
            end
    
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.