0

I want to ask if there is anyway to implement a program that can generate IDs with dot seperator "." like for example:

a1.b2.c3 

Note that I don't want to dot to be dealt as a character, it should be like a seperator.

The same if you put a dot between your name and your father's name and your grandfather's name, like:

John.Paul.Hit
Andrey
  • 2,503
  • 3
  • 30
  • 39
Gloria
  • 329
  • 4
  • 17

3 Answers3

6

You can do this already, as pointed out. HOWEVER, you should recognize that it will be less efficient

A = 3;
B.C.D.E = 3;

whos A B
  Name      Size            Bytes  Class     Attributes

  A         1x1                 8  double              
  B         1x1               536  struct              

See that B took up MUCH more storage than did A.

Also, you need to recognize that A.B and A.C are not different objects in MATLAB, but part of the same struct, A. In fact, if I try to create A.B now, it will get upset, because A already exists as a double.

A.B = 4
Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures
Displays Warning, for details. 
A = 
    B: 4

The original variable A no longer exists.

There are also time issues. The struct will be less efficient.

timeit(@() A+2)
Warning: The measured time for F may be inaccurate because it is close to the estimated time-measurement overhead (3.8e-07 seconds).  Try measuring
something that takes longer. 

> In timeit at 132 
ans =
    9.821e-07

timeit(@() B.C.D+2)
ans =
   3.6342e-05

See that adding 2 to A is so fast that timeit has trouble measuring it. But adding 2 to B.C.D takes something on the order of 30 times as long.

So, in the end, you MAY be able to do what you want with a struct, but there are good reasons for not doing so, unless you have a very valid need for the dot. An alternate separator works better in the respects I've shown.

A = 3;
A_B_C_D = 3;
whos A*
  Name         Size            Bytes  Class     Attributes

  A            1x1                 8  double              
  A_B_C_D      1x1                 8  double              

Computations will be equally fast with either of these variables.

  • +1 performance evaluation. One application OP could use struct is in [function parameters](http://stackoverflow.com/a/11907190/957997). Specially if many of them are required :) – Yamaneko Oct 23 '12 at 01:56
2

Matlab already uses the dot as a separator in ids, specifically in ids for structures and their fields. For example, executing

a.b = 3

creates a structure called a with a field called b which itself has value 3. Read the documentation on the topics of structures and the function struct.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
0

Not in the way you want. As the above answers note, the dot has a particular meaning in ML syntax, and can't be used as part of the identifier itself.

Marc
  • 3,259
  • 4
  • 30
  • 41