JuliaLang/julia

is there a equivalent thing as Matlab struct?

Closed this issue · 14 comments

I am very interested in Julia Language and just begin to learn, and want to know if there is something like Matlab Structure, or those kind of feature or Structure Arrays could be implemented in some kind of dynamic modification of Type definitions?

No, we don't have anything exactly equivalent to a matlab struct array.

If the number of fields is very large, a hash table of symbols makes sense. If the number of fields is small, one could define a type with all the needed field names and use that.

Or, the "julia way" is to define a StructArray type yourself that behaves how you want. It could keep a list of field names, and implement adding and deleting fields, indexing, etc. This should be no slower than matlab's struct arrays since it has to do the same operations.

Thanks for the clarification. i think the StructArray type could be base on dictionary type or just a array of Tuple{String, Any}, hope it would be sit in the standard library soon!

I would strongly consider using either just a Dict or a composite type. The Matlab struct business is a really kind of a compromise that's somewhere between those two and not obviously better than either at any particular job. I have a hard time thinking of use cases where a StructArray would be better. Keep in mind that "no slower than matlab's struct arrays" still means "much slower than Julia's composite types" and possibly even "slower than Julia's dicts" since both of those are quite fast and well-tuned.

The matlab struct array is a really wacky data structure. It's fine when you just want a small dictionary with string keys, but its full behavior is crazy. I've implemented it before and we didn't know whether to laugh or cry. I would think hard about what the use cases are before implementing this.

To me a main attraction of the Matlab struct is the easy access into hierarchical structs, e.g.

a.b.c.d

If it's practical to implement with composite types Julia code would look the same, but if it's necessary to resort to dictionaries the access pattern would instead be

a["b"]["c"]["d"]

which is comparatively painful both to write and read.

Nesting composite types is no problem. I agree that a["b"]["c"]["d"] is ugly, not that I haven't written tons of code in dynamic languages that does precisely that.

The actual nesting of composite types is no problem but in situations where you need to nest dozens of different types to represent your data, defining the types can be a substantial effort.

i think the matlab structure is some kind of syntax suger of dictionary. if you use a composite type then you cann't dynamicly add or delete it's fields or using Struct.(string expression) to generate hierarchical structs. if you use a straight dictionary then you get ugly and tedious code. matlab struct could be use to represent dynamic data structures clean and clear. for example, all possible experimental data organized in hierarchy as the standard data structure. then the subsequent analysis code could adapt to different subset of data fields through checking existence of particular relevent fields.

If the . operator is overloadable, you could just define a . operator function that takes in a dict and your key to search and perform the dict lookup there, if you really wanted to.

As far as I know, . is not overloadable. I hope it will be.

I rather would like it to be, but that presents a number of implementation and performance issues, so it may take some time before that can be done.

Since this decision is no longer about the original subject at all, I'm closing it and opening a new issue.

Has any work been one on this ? e.g. , is there now any functionality in julia that allows the use of MATLAB like structures?