Loading and naming specific columns
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I used this code to import and name data in Matlab:
for i=1951:2000
fileName=['arunoff_' num2str(i)];
dataStruct.(fileName)=importdata([fileName '.txt']);
end
This creates data structure dataStruct.arunoff_(i).data for each year (1951-2000). Next I would like to create and name another set of variables using the third column in 'data' (i.e. dataStruct.arunoff_1951.data(:,3), dataStruct.arunoff_1952.data(:,3) etc.). However, I don't know how to write a loop that does that and was wondering if someone could help me with this? My main problem is I don't know how to tell the loop to use data in each a_runoff(i) structure.
Hope this makes sense, thanks very much for the help!
0 个评论
采纳的回答
Walter Roberson
2011-8-30
fn = fieldnames(dataStruct);
for K = 1:length(fn)
newStruct.([fn 'd3']) = dataStruct.(fn).data(:,3);
end
2 个评论
Andrei Bobrov
2011-8-30
Hi Walter!
Little typo.
for K = 1:length(fn)
newStruct.([fn{K} 'd3']) = dataStruct.(fn{K}).data(:,3);
end
Walter Roberson
2011-8-30
Good point. I mentally started with K = fn (i.e., iterate over the cells) and didn't clean up properly afterwards.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!