How can I rename a structure name inside a loop?

  • Follow


Hi 

I have a database of structures with similar fields but different names (for simplicity, assume that there are 3 structures named, "John", "Mike", and "Sara" all of which have three similar fields: id, address, title).  In my code, user can input as much new structures (with similar fields) as she/he wants with any arbitrary name.  I want to read the information in these structures and work on them inside a for loop like this:

for i=1:number of new structures
 ...
 if strcmp(John.id , first structure input by the user.id) == 1
  do ...
 end
 ...
end

But I'm not able to change the name of the structures inside the loop as "i" changes.  For example, if the user inputs "Megan" as the name of her/his first structure and then inputs "Bobby" as the second one, how can I change "Megan.id" to "Bobby.id" inside the loop?  Is there a function in MATLAB for this purpose?  I tried saving the new structures under a universal name and then use 'eval' function but it didn't work.
   
Thank you
Hadi
0
Reply Hadi 3/24/2011 2:58:04 AM

On 3/23/2011 7:58 PM, Hadi wrote:
> Hi
>
> I have a database of structures

What is a "database of structures" ? Is this a term you came up, I do not see
in a matlab a type called "database"?

> with similar fields but different names (for simplicity,
>  assume that there are 3 structures named, "John", "Mike", and "Sara"

What do you mean a structure  named "Mike" ?  How can a structure have a name
which a string "Mike" ?

> all of which have three
>  similar fields: id, address, title).  In my code, user can input as much new structures (with
>  similar fields) as she/he wants with any arbitrary name.  I want to read the information in these
> structures and work on them inside a for loop like this:
>
> for i=1:number of new structures
>   ...
>   if strcmp(John.id , first structure input by the user.id) == 1
>    do ...
>   end
>   ...
> end
>
> But I'm not able to change the name of the structures inside the loop as "i" changes.
>For example, if the user inputs "Megan" as the name of her/his first structure and then
>  inputs "Bobby" as the second one, how can I change "Megan.id" to "Bobby.id" inside the loop?
>  Is there a function in MATLAB for this purpose?  I tried saving the new structures under
>  a universal name and then use 'eval' function but it didn't work.
>
> Thank you
> Hadi

I think you are using some terminology that I am not familar with.

I think what you are trying to do is to make an array of records?

So, In each structure (or a record, same thing), you have fields.
One of them will be the name of the person.

In Matlab, this corresponds to a cell array, in which
each cell is a structure.

------------------------------
database = cell(1,10);  %make 'database' which contains 10 records

% populate the database

database{1}=struct('name','john','age',10,'id',100);
database{2}=struct('name','mikey','age',90,'id',1009)

etc...

%access the 'database'

>> database{1}
     name: 'john'
      age: 10
       id: 100

>> database{2}
     name: 'mikey'
      age: 90
       id: 1009

-----------------------------

Now, you can make a loop, which iterate over 'database' variable, which is
a cell array, looking at each record and see if the name is what you want,
and do anything you want.


--Nasser
0
Reply Nasser 3/24/2011 3:19:25 AM


On 3/23/2011 7:58 PM, Hadi wrote:
> Hi
>


> for i=1:number of new structures
>   ...
>   if strcmp(John.id , first structure input by the user.id) == 1
>    do ...
>   end
>   ...
> end
>

an example to use loop over the 'database'

----------------------
N=10;
s=struct('name','','age',0,'id',0);

database = cell(1,N);  %make 'database' which contains 10 records

% populate the database
i=1;
s.name='john'; s.age=10; s.id=100;
database{i}=s;

i=i+1;
s.name='mikey'; s.age=90; s.id=1009;
database{i}=s;

max_items = i;
for k=1:max_items
    if database{k}.id==100
       database{k}.name='steve';
    end
end

----------------------------

--Nasser
0
Reply Nasser 3/24/2011 3:35:24 AM


"Hadi " <hnbokaee@yahoo.com> wrote in message 
news:imebvs$msk$1@fred.mathworks.com...
> Hi
> I have a database of structures with similar fields but different names 
> (for simplicity, assume that there are 3 structures named, "John", "Mike", 
> and "Sara" all of which have three similar fields: id, address, title). 
> In my code, user can input as much new structures (with similar fields) as 
> she/he wants with any arbitrary name.  I want to read the information in 
> these structures and work on them inside a for loop like this:

Don't do this. See the question in the Programming section of the newsgroup 
FAQ about creating variables A1, A2, etc. for an explanation why this is a 
bad idea and alternatives you should use instead.

-- 
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 

0
Reply slord (13366) 3/24/2011 3:10:06 PM

3 Replies
541 Views

(page loaded in 0.065 seconds)

Similiar Articles:













7/24/2012 12:52:17 AM


Reply: