Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create new name for a structure or variable 1

Status
Not open for further replies.

Maer

Automotive
Dec 12, 2004
1
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
 
Replies continue below

Recommended for you

You just have to remove your last [tt]num2str[/tt] 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 [tt]anyX[/tt] 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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor