best way to import multiple sets of data?
22 次查看(过去 30 天)
显示 更早的评论
I'm trying to import data from 26 separate files into the variables u and v, and I want 26 separate instances each of u and v (ie u_1 through u_26). In the files, u and v are 29x39 doubles. I tried the following code to create u and v as 3D arrays:
for k=1:26
filename = sprintf('Angle1-%d.mat', k);
data = load(filename);
u = zeros(29, 39, 26);
v = zeros(29, 39, 27);
u(:, :, k) = data.u;
v(:, :, k) = data.v;
This made sense to me conceptually but it didn't seem to work in practice - the whole array ended up being zeros except for the last set of 29x39. Is there a better way to import this data?
3 个评论
Guillaume
2015-11-25
Tushar, why don't you write your comment as an answer so that:
- Emily can accept your answer
- you get reputation points
- the question can be marked as answered
回答(1 个)
Tushar Athawale
2015-11-25
编辑:Tushar Athawale
2015-11-25
I understand that you want to import multiple 2-D datasets and store them in a 3-D matrix. The approach you are following is right except for the fact that the initialization of 'u' and 'v' arrays with zeros should be done only once outside the for loop. For example, your code should be modified as follows:
u = zeros(29, 39, 26);
v = zeros(29, 39, 27);
for k=1:26
filename = sprintf('Angle1-%d.mat', k);
data = load(filename);
u(:, :, k) = data.u;
v(:, :, k) = data.v;
end
In your code, since you are initializing 'u' and 'v' arrays with zeros in each iteration of a for loop, the data added to these matrices is getting wiped out with zeros and only data in the last iteration is stored as control comes out of the for loop.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!