Equivalent of MATLAB struct with subfields and constant values

402 views
Skip to first unread message

Pooya

unread,
Feb 20, 2015, 8:48:24 PM2/20/15
to julia...@googlegroups.com
Hi there,

I am new to Julia, trying to transfer all I do from MATLAB to Julia. I have a bunch of matrices of numerical data (say 100x30). In order to keep track of what each column is representing, I defined different variables (say flows) whose values were the associated column number in MATLAB. Now I can use data(:,flows) instead of data(:,X), so I don't need to worry about the column orders and numbers every time I need to use the data. In order to avoid mistakes I was using "struct" in MATLAB to avoid introducing many new variables into each code that I have. Then all those constant values (column numbers) were defined in a function (say myconstants) and when I needed to use them, I would just say C = myconstants, and then use C.flow for the column number. The problem is that there are a few categories of these constants, and they all have subfields with meaningful names and values representing the column number in each matrix of data. In MATLAB all I needed to define these was for example, C.flows.out = 5. 

I am now thinking of the best way to handle this in Julia. I have learnt a bit about immutable composite types. But first, because they do not accept default values, the code will be very messy with a lot of different fields. I am also not sure if you can define the fields of an immutable to be an immutable type itself. Is that possible? Any ideas on better ways to handle this situation? In addition, if I use composite types it seems that I am using a very powerful thing (composite types) for a very basic application! Any thoughts are greatly appreciated.

Thanks,
Pooya

Viral Shah

unread,
Feb 21, 2015, 1:26:43 AM2/21/15
to julia...@googlegroups.com
You could explore DataArrays.

If you do not need to otherwise manipulate the whole array directly (like say do an svd on the 100x30 matrix), and only work with the fields, it would be best to create a composite type with those field names.

-viral

Milan Bouchet-Valat

unread,
Feb 21, 2015, 5:08:44 AM2/21/15
to julia...@googlegroups.com
Le vendredi 20 février 2015 à 22:26 -0800, Viral Shah a écrit :
> You could explore DataArrays.
> https://github.com/JuliaStats/DataArrays.jl
I guess you mean DataFrames? Indeed, the column names would provide the
equivalent of your variables/subfields.

flows.out wouldn't be possible, but you can always create a names like
flows_out, flows_in... to keep a mental representation of categories. Is
that enough for you?

> If you do not need to otherwise manipulate the whole array directly
> (like say do an svd on the 100x30 matrix), and only work with the
> fields, it would be best to create a composite type with those field
> names.
OTOH if you need a real array, not a set of columns, you can explore
NamedArrays instead of DataFrames.


Regards

Pooya

unread,
Feb 24, 2015, 10:03:19 AM2/24/15
to julia...@googlegroups.com
Thanks for your suggestion. DataFrames should work well for my application. I am not sure about the differences of NamedArrays and DataFrames. I can see that in NamedArrays one can name rows as well. Is that all? Or are there other reasons why one might use one instead of the other?

Milan Bouchet-Valat

unread,
Feb 24, 2015, 10:12:02 AM2/24/15
to julia...@googlegroups.com
Le mardi 24 février 2015 à 07:03 -0800, Pooya a écrit :
> Thanks for your suggestion. DataFrames should work well for my
> application. I am not sure about the differences of NamedArrays and
> DataFrames. I can see that in NamedArrays one can name rows as well.
> Is that all? Or are there other reasons why one might use one instead
> of the other?
That's kind of the same difference as between DataArray and DataFrame. a
NamedArray simply wraps an array by adding names to it, while a
DataFrame is a set of vectors which have each their own element type.
DataFrames are intended for database-like data, while arrays are useful
when you want to use matrix algebra and things like that.


Regards
Reply all
Reply to author
Forward
0 new messages