How does one create a dataset from a large array and subsequently name the variables?
7 views (last 30 days)
Show older comments
I have a body data array read from a excel file: 9000-by-130. I also have the header line read from the same file: 1-by-130. I am trying to create a 900-by-130 dataset from the data array while using the elements of the header line array as variable names.
data = <9000x130 dataset>
headers = <1x130 cell>
The issue I'm having is...
D = dataset(array(:));
...just creates a 9000-by-1 dateset!? So, for example,
for i = 1:size(headers,2)
dataSet.Properties.VarNames{i} = headers{i};
end
...cannot find variables to match elements in headers{i}.
The only solution is to enter the variables and the names manually 130 times!
dataSet = dataset(data(:,1),data(:,2)...data(:,130),'VarNames',{'Var1','Var2',...,'KillMeNow'});
Surely there's a simpler way. Is there?
0 Comments
Accepted Answer
Peter Perkins
on 18 Sep 2012
Tolulope, your question is not entirely clear. You show
data = <9000x130 dataset>
headers = <1x130 cell>
and it's not clear if that's what you have, or if that's what you want.
If that's what you have, and headers is a 1x130 cell array of strings, why does
data.Properties.VarNames = headers
not work? What does, "cannot find variables to match elements in headers{i}" mean?
You have an Excel file. You have said that you cannot use the dataset constructor to read the file and get the dataset array you want. It's not clear why that would be. Perhaps the header line is not in the obvuious place. The above should solve that.
This
D = dataset(array(:));
passes a column vector to the dataset constructor, so it's not unexpected that it creates a (something)x1 dataset array. It's not clear to me what "array" is, so it's impossible to know exactly what "something" is, or what you intended to do with that line.
If you have a 9000x130 numeric array, and a 1x130 cell array of strings, then as described in the help for dataset, you can do this:
x = rand(10,5);
d = dataset({X,names{:}})
Or, you can use use num2cell to split your numeric array up columnwise:
x = rand(10,5);
y = num2cell(x,1);
d = dataset(y{:},'VarNames',names)
If you have a very recent version of MATLAB, you can use the mat2dataset function (I can't recall if this exists in R2012a, but it definitely does in R2012b).
Hope this helps.
3 Comments
More Answers (2)
Wayne King
on 18 Sep 2012
Why don't you organize the excel file and then create the dataset directly from the excel file with the
A = dataset('XLSFile',filename,...
syntax?
Javier
on 18 Sep 2012
Hello Tolulope
This is what you have to do. In this case, data has 5 columns.
Step 1 (define the names)
for i=1:size(data,2)
VarNames{1,i}=['Var ',num2str(i)];
end
Step 2 (introduce in the data set)
dataSet = dataset(data(:,1),data(:,2),data(:,3),data(:,4),data(:,5),'VarNames',VarNames);
If this solve your question please grade or post a comment.
Best regards
Javier
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!