Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski 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
Joined
Dec 12, 2004
Messages
1
Location
SE
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
 
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

Back
Top