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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:58:44+00:00 2026-05-26T20:58:44+00:00

I have a class that is by handle, and I want to copy it

  • 0

I have a class that is by handle, and I want to copy it by value and store it somewhere. However, if I change anything in the class, it also changes the copy of it.

Here is an example of my class I want to copy:

classdef MyClass < handle
    properties
        data;
    end
    methods
        function M = MyClass()
           M.data=5; 
        end
    end      
end

Here is a test class to test changes:

classdef Test < handle  
    properties 
        store 
    end 
    methods 
         function N = Test(M) 
             N.store = M; 
         end 
    end 
end 

Now, we create an instance of MyClass and store it in Test:

>> m=MyClass
m =
  MyClass handle
  Properties:
    data: 5
  Methods, Events, Superclasses

>> a=Test(m)
a = 
  Test handle
  Properties:
    store: [1x1 MyClass]
  Methods, Events, Superclasses

>> a.store
ans =
  MyClass handle
  Properties:
    data: 5
  Methods, Events, Superclasses

Finally, if I change the value of ‘data’ in ‘m’ I do NOT want it to change the value in store, however it seems like MyClass is stored in ‘store’ by reference:

>> m.data=3;
>> a.store
ans = 
  MyClass handle
  Properties:
    data: 3
  Methods, Events, Superclasses

>> a.store.data
ans =
     3

Is it possible to copy a “handle” class? Or do I need to change my class to make by value for this to work?

  • 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-26T20:58:44+00:00Added an answer on May 26, 2026 at 8:58 pm

    You can make it copyable by deriving from matlab.mixin.CopyableClass, see the link for details.

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

Sidebar

Related Questions

I have written a class that will handle internal logging in my application. Now
I have a class that implements properties in a specific way, to handle some
I have a Sms Broadcast Receiver class that extends broadcastreceiver to handle incoming sms,
I want to have class that can be instantiated with list, array, seq, set,
I have a class that implements ISelectionListener. I want to determine when the user
I have a class that reads a pop3 email server. I want to write
I have two class, one is an activity that can handle Chronometer, another is
I have an abstract Handle<T> class that contains references an objects of type T.
I'm creating a class that handles HTTP connections, and I want to handle both
I have a class that implements Runnable. For logging reasons I want to know

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.