Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is there a equivalent thing as Matlab struct? #1263

Closed
babaq opened this issue Sep 7, 2012 · 14 comments
Closed

is there a equivalent thing as Matlab struct? #1263

babaq opened this issue Sep 7, 2012 · 14 comments

Comments

@babaq
Copy link

babaq commented Sep 7, 2012

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?

@JeffBezanson
Copy link
Member

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.

@babaq
Copy link
Author

babaq commented Sep 7, 2012

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!

@StefanKarpinski
Copy link
Member

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.

@JeffBezanson
Copy link
Member

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.

@GunnarFarneback
Copy link
Contributor

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.

@StefanKarpinski
Copy link
Member

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.

@GunnarFarneback
Copy link
Contributor

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.

@babaq
Copy link
Author

babaq commented Sep 8, 2012

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.

@staticfloat
Copy link
Member

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.

@tshort
Copy link
Contributor

tshort commented Sep 27, 2012

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

@StefanKarpinski
Copy link
Member

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.

@StefanKarpinski
Copy link
Member

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

@huckl3b3rry87
Copy link

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

@ChrisRackauckas
Copy link
Member

@huckl3b3rry87 See #16580

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants