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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:49:10+00:00 2026-05-25T16:49:10+00:00

I want the following code to set the ‘name’ field of the myClass instance

  • 0

I want the following code to set the ‘name’ field of the myClass instance to be equal to ‘super_awesome_name’:

super_awesome_name = myClass();

where the myClass constructor is as follows:

function obj = myClass()
    obj.name = ???;
end

Is there some way to do this in Matlab? Right now I can accomplish something very similar with assignin, but it makes the syntax very unnatural:

 myClass(super_awesome_name);

where the myClass constructor is as follows:

function obj = myClass(var_name)
    obj.name = assignin('caller',var_name, obj);
end
  • 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-25T16:49:11+00:00Added an answer on May 25, 2026 at 4:49 pm

    As far as I know, there is no convenient way of doing so. You can see this yourself since MATLAB will have to perform some when constructing an object like that.

    1. First construct the object
    2. Store that object in super_awesome_name

    But step 1 needs information from step 2 (i.e. the name of the variable). So you will have to work around that.

    You solution may work, but it leaves a lot of room for error. Just consider the case when super_awesome_name is undefined before the construction: this will throw an exception due to an unitialized variable.

    IMHO it is easier to make a constructor that accepts a string containing the name (or possibly a variable, as you can get the variable name of an argument by using inputname). Using inputname, however is just a bit clearer than using assignin.

    Why do you need such functionality? I’m thinking of a situation where you might do something like:

    x = myObjectThatHasAName(); % x.name contains 'x'
    y = myObjectThatHasAName(); % y.name contains 'y'
    x = y;                      % x.name contains 'y' !
    

    So that might be the behavior you are looking for, but it may become very confusing if you pass the variables along a lot. It looks to me like you are mixing debugging information into your actual computations.

    If you need the functionality to determine the actual variable name in another method or function, just use inputname. Then you won’t need to keep track of the variable name yourself with all confusing consequences. That would be what I’d propose to do in most cases. You can see an example in the MATLAB documentation.

    On the other hand, I think the clearest way to store the variable name inside the object, could be something like:

    classdef myObjectThatHasAName
       properties
          name = '';
       end
       methods
          function obj = myObjectThatHasAName()
          end
          function obj = storeName(obj,name)
             if nargin < 2
                name = inputname(1);
             end
             if ~isempty(name) % && isempty(obj.name)
                obj.name = inputname(1);
             end
          end
       end
    end
    

    I haven’t tested this (lack of MATLAB on my current computer), but the following code should do it:

    x = myObjectThatHasAName();
    x = x.storeName(); 
    % or x = storeName(x);
    % or x = storeName(x,'x');
    

    If you don’t mind the overhead, you could always incorporate the storeName into each other method (and forget about calling storeMethod outside of the methods). This keeps track of the variable name where the object resided before the last assignment. A possible method might look like this:

    function obj = otherMethod(obj,arguments)
       obj = obj.storeName(inputname(1));
       % actual code for the method
    end
    

    That way the name is set when you first call any method on the object (this almost does what you want, but I guess it will do what you need in most cases). You can of course adjust the code to just remember the first assignment (just comment out the code in storeName).

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

Sidebar

Related Questions

In the following code, I want to set Onclick Listeners on some elements of
In the following code I want to use the dilate function but I don't
So I have the following code in a method which I want to set
I want to set file length to zero. i used following code try {
I currently have the following code set up for a jQuery slide. $(document).ready(function ()
I have the following code which I use to set HttpRequest querystring. I want
I am trying to set system date (not time) using following code. I want
In the following code I want to set standalone = yes to the xml,
I am using following code and want to know whether we require to set
I want to set maximum length to the textarea. I am using following code

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.