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: How can I rename a structure name inside a loop? - comp.soft-sys ...Hi I have a database of structures with similar fields but different names (for simplicity, assume that there are 3 structures named, "John", "Mik... Can't rename file?` - comp.unix.solarisHow can I rename a structure name inside a loop? - comp.soft-sys ... Can't rename file?` - comp.unix.solaris How can I rename a structure name inside a loop? - comp.soft ... imported file name - comp.databases.filemaker... you can import it with filemakers impaired import function(s). Loop ... Loop through the list and do: a. rename/copy file to a known location (You can get ... inside the loop ... /bin/sh: Local variables in a function ? - comp.unix.solaris ...You can fix this to some extent by using funny names: myfunc ... local variable inside the function. I know ... comp.lang.awk How to rename fields in structure ... Directory creation and file moving by modification dates? - comp ...This information is inside the file and less ... And if you want your simpler directory structure you ... > Alternatively, how can I batch rename images in a working ... Dynamic column name in function - comp.databases.mysqlHow to use a popup menu value inside a function in a GUI ... ... rename worksheets - comp.soft-sys.sas Dynamic column name in function - comp.databases.mysql RENAME ... for loop variable scope - comp.lang.tclIn an old script there is $mh(hold_fh) insert $msg rename ... function - comp.soft-sys.matlab Nested Function: Loop index set in child function - Newsreader ..... the name to ... Assigning Variables in a parfor loop - comp.soft-sys.matlab ...... indexing syntax S.var1, S.var2, etc... you can unpack the structure S ... yalmip - comp.soft-sys.matlab For instance, I can't even define a Yalmip variable inside a parfor loop. renaming variables in preset MAT files - comp.soft-sys.matlab ...... because the original variable name is inappropriate). And of course, I can't ... for loop variable scope - comp.lang ... that you wrote that is named cd, rename or ... function ... sort multiple images - comp.soft-sys.matlabwhy dont you Just form the image name in loop ... where ypu sort on the length of the name field. Use of a comparision function ... solutions: - write a script to rename ... How can I rename a structure name inside a loop? - comp.soft-sys ...Hi I have a database of structures with similar fields but different names (for simplicity, assume that there are 3 structures named, "John", "Mik... SSIS Foreach Loop to copy, rename files within foldersSSIS Foreach Loop to copy, rename files within folders ... that you are getting the same file structure ... This may require another for each loop inside of ... 7/24/2012 12:52:17 AM
|