best way to import multiple sets of data?

27 ビュー (過去 30 日間)
Emily Bell
Emily Bell 2015 年 11 月 19 日
編集済み: Tushar Athawale 2015 年 11 月 25 日
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
Guillaume 2015 年 11 月 25 日
Tushar, why don't you write your comment as an answer so that:
  1. Emily can accept your answer
  2. you get reputation points
  3. the question can be marked as answered
Tushar Athawale
Tushar Athawale 2015 年 11 月 25 日
Thank you Guillaume.

サインインしてコメントする。

回答 (1 件)

Tushar Athawale
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.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by