Headers in Matlab

Hello, my problem is the following one. I have data say of the following kind:
Iter X Y Z
1 30 40 50
2 50 60 70
3 30 70 80
I want to be able to store the data in different columns by invoking the headers. To explain it better, if the variable "data" contains all the information, then when I say data['Iter'] I should be able to obtain the column [1,2,3]. I have learnt this from python and I want to know if this is possible using Matlab.
Chiara

 Accepted Answer

Chiara Modenese
Chiara Modenese on 21 Mar 2011
Thanks to all but I think that only Walter got my question. I know about structures. However, my task is to save a column variable by only knowing the name of the corresponding header. In the end I should be able to write something like
iter=data('iter')
This is very useful when you import different files which share similar headers but where similar variables have different positions. I would simply like to avoid to retrieve the number of the column of each variable am interested in from each single file. If we could remember the name of the variables (like iter), then it would be a peace of cake to store what we want.
Chiara

5 Comments

Walter Roberson
Walter Roberson on 21 Mar 2011
You can use dynamic field names.
for K = 1 : length(headerfields)
thisname = headerfields{K};
data.(thisname) = datavalues{K};
end
You can now retrieve by data.Iter (for example) without having to know which column it occurred in.
Chiara Modenese
Chiara Modenese on 21 Mar 2011
Thank you, this answered my question. To be more precise, the third line of your code should be:
data.(thisname{1})
given that headerfields is a cell array (at least is so in my case).
Chiara
Walter Roberson
Walter Roberson on 21 Mar 2011
Only if you have a cell array of cell arrays; the more usual case of a cell array of strings would use the code I showed.
Matt Tearle
Matt Tearle on 21 Mar 2011
If it's coming from textscan, it might well be as a cell array of cell arrays.
Chiara, do you have Statistics Toolbox? Because if you do, it really sounds like dataset arrays will make your life easier.
Yes, sorry, I used a cell array of cell arrays as you point out.
And yes, Matt, I have that tool. I will try to use dataset instead of structures, as it seems it would simply my task a lot.

Sign in to comment.

More Answers (3)

Matt Tearle
Matt Tearle on 21 Mar 2011

3 votes

Perhaps dataset arrays are what you're looking for?
The syntax isn't exactly what you've specified, but you can refer to columns by name, similar to structures. You can read data from file (including column headers) directly with dataset.
Walter Roberson
Walter Roberson on 20 Mar 2011

0 votes

You will not be able to do what you want with that syntax. You would be able to use data('iter'), if you went through a long exercise of programming a new data class. Or you could go for the Database Toolbox.
Andrew Newell
Andrew Newell on 20 Mar 2011
You can do something similar using structures:
data.Iter = [1; 2; 3];
data.X = [30; 50; 30];
data.Y = [50 60 70];
data.Z = [50 80 80];
data.Iter
ans =
1
2
3

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!