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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:16:29+00:00 2026-05-28T14:16:29+00:00

In matlab, I can create a structure array (struct) by doing the following. person.name

  • 0

In matlab, I can create a structure array (struct) by doing the following.

person.name = 'Mike';
person.age = 25;
person.gender = 'male';

wherein person is not defined prior to creating the struct. When I try to do it in python, it gives me an error

name 'person' is not defined

Is there a similar way of doing it in python? Thanks

EDIT: Being new to python, I still think in matlab. The problem that I have is that I have a function that will take multiple inputs (more than 40), so instead of having function(input1,input2,…,input40), I will just have function(input) where the input consist of input.1, input.2,.., input.40.

  • 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-28T14:16:30+00:00Added an answer on May 28, 2026 at 2:16 pm

    Python won’t magically create a container object when you start assigning attributes to it, and if matlab allows this, I’d consider matlab badly broken. Consider this:

    person.name = "Mike"
    persom.age  = 25
    person.sex  = "Male"
    

    Now we have two objects, person and persom, and person doesn’t have age, and there was no hint that this happened. Later you try to print person.age and, one would hope, matlab then complains… two pages after the actual mistake.

    A class can itself be used as a container or namespace. There’s no need to instantiate it, and it’ll save you a bit of typing if you just want a bundle of attributes.

    class sex:
        male   = "M"
        female = "F"
    
    class person:
        name = "Mike"
        age  = 25
        sex  = sex.male
    

    To access or modify any of these, you can use person.name, etc.

    N.B. I used a class for sex as well to illustrate one of the benefits of doing so: it provides consistency in data values (no remembering whether you used “M” or “Male” or “male”) and catches typos (i.e. Python will complain about sex.mlae but not about the string “mlae” and if you were later checking it against “male” the latter would fail).

    Of course, you still run the risk of misspelling name, age, or sex in this type of class definition. So what you can do is use the class as a template and instantiate it.

    class Person:
        def __init__(self, name, age=None, sex=None):
            self.name, self.age, self.sex = name, age, sex
    

    Now when you do:

    person = Person("Mike", 25, sex.male)
    

    or if you want to document what all those parameters are:

    person = Person("Mike", age=25, sex=sex.male)
    

    it is pretty much impossible to end up with an object that has a misspelled attribute name. If you mess it up, Python will give you an error message at the point you made the mistake. That’s just one reason to do it this way.

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

Sidebar

Related Questions

I know I can create a 3D surface plot in MATLAB by doing: x
How can I create a function with MATLAB so I can call it any
If I use the inline function in MATLAB I can create a single function
how can i create symbolic variables in matlab 2009? because it doesn't work for
http://www.mathworks.com/matlabcentral/answers/1325-what-is-missing-from-matlab#answer_1931 The basic jist is you can't create a matrix and directly index it.
Scatter Plots in MatLab. I can create a scatter plot with x and y
I want to create an image database which can be entirely accessed via MATLAB.
I know that you can create a file browser in Matlab using uigetfile. However,
I'have to create these three simple squares in Matlab: Can anyone give me a
In MatLab, you can declare symbols pretty easily: syms a,b mat = [a,b] I'm

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.