×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Create new name for a structure or variable

Create new name for a structure or variable

Create new name for a structure or variable

(OP)
Hi

I want to create new names for my structure, or if not possible, variables. I have the following problem:

A variable in a for-loop is to be a part in my structure name. This means that the structures chuold be named as; structure1, structure2 and so on.

Is there any way to this and to be able to store any kind of data into these structures?

I have earlier tried to make variables in the same way using the following;ยจ

for i=1:10 eval(['variable' num2str(i) '=' num2str(i)]); end

 but in this case it is not possible to store numerical data in any good way, only strings.

Does anyone have any idea on how to solve this?


Regards Mattias  

RE: Create new name for a structure or variable

You just have to remove your last num2str to be able to assign any kind of data to your variable.

CODE

eval(['variable' num2str(i) '= anyX']);
And if you want this variable to be part of a struct you only have to add the structname to the expression

CODE

eval(['structname.variable' num2str(i) '= anyX']);
In both cases the anyX variable can contain anything. A matrix, a cell, a string or a double, it doesn't matter what kind of datatype is stored in the variable.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources