best way to import multiple sets of data?

25 visualizaciones (últimos 30 días)
Emily Bell
Emily Bell el 19 de Nov. de 2015
Editada: Tushar Athawale el 25 de Nov. de 2015
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 comentarios
Guillaume
Guillaume el 25 de Nov. de 2015
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 el 25 de Nov. de 2015
Thank you Guillaume.

Iniciar sesión para comentar.

Respuestas (1)

Tushar Athawale
Tushar Athawale el 25 de Nov. de 2015
Editada: Tushar Athawale el 25 de Nov. de 2015
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.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by