How does one create a dataset from a large array and subsequently name the variables?
显示 更早的评论
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?
采纳的回答
更多回答(2 个)
Wayne King
2012-9-18
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
2012-9-18
0 个投票
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
类别
在 帮助中心 和 File Exchange 中查找有关 Environment and Settings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!