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 8410021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:04:25+00:00 2026-06-10T00:04:25+00:00

Writing a subclass of dynamicprops allows to me to add properties dynamically to an

  • 0

Writing a subclass of dynamicprops allows to me to add properties dynamically to an object:

addprop(obj, 'new_prop')

This is great, but I would also love to create set / get functions for these properties on the fly. Or analysis functions that work on these dynamic properties.

My experience with Matlab has been so far, that once I create an instance of a class, adding new methods is not possible. That is very cumbersome, because my object may contain a lot of data, which I’ll have to re-load every time that I want to add a new method (because I have to do clear classes).

So is there a way to add methods on the fly?

  • 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-06-10T00:04:26+00:00Added an answer on June 10, 2026 at 12:04 am

    You cannot add methods like you add dynamic properties. However, there are two ways for implementing new methods during development that won’t require you to re-load the data every time.

    (1) I write standard methods as separate functions, and call them as myMethod(obj) during development. Once I’m sure they’re stable, I add their signature into the class definition file – this requires a clear classes, of course, but it is a much delayed one, and from time to time you may have to shut down Matlab, anyway.

    (2) With set/get methods, things are a little trickier. If you are using dynamicprops to add new properties, you can also specify their set/get methods, however (most likely, these methods/functions will want to receive the name of the property so that they know what to refer to):

    addprop(obj,'new_prop');
    prop = findprop(obj,'new_prop');
    prop.SetMethod = @(obj,val)yourCustomSetMethod(obj,val,'new_prop')
    

    EDIT

    (2.1) Here’s an example of how to set up a hidden property to store and retrieve results (based on jmlopez’ answer). Obviously this can be improved a lot if you have a better idea what you’re actually designing

    classdef myDynamicClass < dynamicprops
        properties (Hidden)
            name %# class name
            store %# structure that stores the values of the dynamic properties
        end
        methods
            function self = myDynamicClass(clsname, varargin)
                % self = myDynamicClass(clsname, propname, type)
                % here type is a handle to a basic datatype.
                self.name_ = clsname;
                for i=1:2:length(varargin)
                    key = varargin{i};
                    addprop(self, key);
                    prop = findprop(self, key);
                    prop.SetMethod = @(obj,val)myDynamicClass.setMethod(obj,val,key);
                    prop.GetMethod = @(obj)myDynamicClass.getMethod(obj,key);
                end
            end
            function out = classname(self)
                out = self.name_;
            end
        end
        methods (Static, Hidden) %# you may want to put these in a separate fcn instead
            function setMethod(self,val,key)
               %# have a generic test, for example, force nonempty double
               validateattributes(val,{'double'},{'nonempty'}); %# will error if not double or if empty
    
               %# store
               self.store.(key) = val;
    
            end
            function val = getMethod(self,key)
               %# check whether the property exists already, return NaN otherwise
               %# could also use this to load from file if the data is not supposed to be loaded on construction 
               if isfield(self.store,key)
                  val = self.store.(key);
               else
                  val = NaN;
               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 am writing a custom subclass of UITableView. I would need this object itself
I am writing a custom NSView subclass. I have several instances of this class
I'm writing a unit test for a custom subclass of org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter<T> and I need
Writing htaccess that allows me to remove index.php from the URL can confuse search
Writing a python program, and I came up with this error while using the
I am writing code that will populate the Margin , Padding and BorderThickness properties
I am writing a webservice that allows users to post files and then retrieve
I'm writing an application which needs to write an object into database. For simplicity,
I'm writing my first NSEntityMigrationPolicy right now and this one is pretty simple. It
I am writing a method for a subclass of NSObject that merges the common

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.