Re: Create Structure
- From: Doug Schwarz <see@xxxxxxxxxxxxxxxxxxx>
- Date: Wed, 30 Jan 2008 16:56:29 GMT
In article
<5dd6376f-fcd5-474e-9f38-4208ab6d0c96@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
shapper <mdmoura@xxxxxxxxx> wrote:
[snip]
Hi,
I haven't use Matlab for 1 year and I am getting back to it.
I have been using a lot of Object Oriented languages such as VB.NET
and C#.
I think the confusion is coming from it ...
For example, in VB.NET I would do something like:
Structure Person
Dim Name As String
Dim Phone As Integer
Dim Country As String
Dim Birthday As String
End Structure
Now I have the structure fields and their types defined.
Then I could create a define any variable of type Person as follows:
Dim Employee_1 As New Person()
Employee_1.Name = "John"
Employee_1.Phone = "0211131"
Employee_1.Country = "US"
Employee_1.Birthday = "10.01.1980"
Dim Employee_2 As New Person()
Employee_2.Name = "Mary"
Employee_2.Phone = "0211231"
Employee_2.Country = "UK"
Employee_2.Birthday = "11.04.1982"
Then I could create a list of Employees
Dim Employees As List Of (Person)
Finally I would add the Employees to my list:
Employees.Add(Employee_1)
Employees.Add(Employee_2)
But as you see first of all I create the structure so I can define its
fields.
But when you write:
Employee_1 = struct('Name',{Bill},'Phone',{234234},'Country',
{France},'Birthday',{21.04.1900});
You create the variable and fill the values all at the same time. And
if by mistake I do something like:
Employee_2 = struct('Name',{Andrew},'Phone',{232343});
Then what happens when I added it to an array?
Sorry, just looking for the best way to do this.
My brain is still working with "well" defined objects.
Thanks,
Miguel
If you have only a few employees to keep track of here is what I would
do:
employees(1).Name = 'John';
employees(1).Phone = '0211131';
employees(1).Country = 'US';
employees(1).Birthday = '10.01.1980';
employees(2).Name = 'Mary';
employees(2).Phone = '0211231';
employees(2).Country = 'UK';
employees(2).Birthday = '11.04.1982';
It's that simple.
If you have many employees to enter then I would allocate the structure
array first. Here is one of many ways to do this:
num_employees = 100;
c = cell(1,num_employees);
employees = struct('Name',c,'Phone',c,'Country',c,'Birthday',c);
and then just fill in the structure array as before.
If you are reading your employee info from a file then I might go about
this in a different way depending on how the info is stored.
--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
.
- References:
- Create Structure
- From: shapper
- Re: Create Structure
- From: Doug Schwarz
- Re: Create Structure
- From: Walter Roberson
- Re: Create Structure
- From: Doug Schwarz
- Re: Create Structure
- From: Walter Roberson
- Re: Create Structure
- From: Doug Schwarz
- Re: Create Structure
- From: shapper
- Create Structure
- Prev by Date: Re: square wave as clock in CR circuit
- Next by Date: Re: simple(ish) matlab calculations (help needed please)
- Previous by thread: Re: Create Structure
- Next by thread: Need Solutions Manual
- Index(es):
Relevant Pages
|
Loading