Hi,
I have 100 model runs and I would like to write out the data. My script looks like this:
for i=1:100 %number of model runs
load(['data' num2str(i) '.mat']);
a1=[x0, x10, x20, x30, x40, x50, x60, x70, x80, x90, x100]; %1x11 matrix
a2=[z0, z10, z20, z30, z40, z50, z60, z70, z80, z90, z100]; %1x11 matrix
y1(i)=[x0(i), x10(i), x20(i), x30(i), x40(i), x50(i), x60(i), x70(i), x80(i), x90(i), x100(i)]; %100x11 matrix
u2(i)=[z0(i), z10(i), z20(i), z30(i), z40(i), z50(i), z60(i), z70(i), z80(i), z90(i), z100(i)]; %100x11 matrix
end
I would like to write out the data in the way as shown in y1 and u1 (each in a 100x11 matrix). Do you have an idea, how to solve this problem?
Thanks a lot!

 Réponse acceptée

Jan
Jan le 10 Août 2017
  1. Do not load variables directly into the workspace. On one hand this confuses the reader, because it is not clear whar a variable came from. On the other hand it impedes Matlab's JIT acceleration, which cannot optimize the access to dynamically created variables.
  2. Do not hide indices in the names of variables. Use indices as indices instead. This sounds trivial, and it is trivialm in fact.
  3. You cannot store a vector in a scalar:
y1(i) = [x0(i), x10(i), x20(i), ...
Perhaps you mean:
y1(i, :) = [x0(i), x10(i), x20(i), ...
4. This is not a " %100x11 matrix":
[x0(i), x10(i), x20(i), x30(i), x40(i), x50(i), x60(i), x70(i), x80(i), x90(i), x100(i)];
but has the size [1 x 11]. Do you mean:
y1(i, :) = a1;
?
Get rid of the hidden indices in the names of the variables. Then:
Folder = cd; % Define the correct folder! Do not rely on CD!
for i=1:100 %number of model runs
Data = load(fullfile(Folder, sprintf('data%d.mat', i));
y1(i, :) = Data.x [???]
end
Now it matters, if you could remove the ugly and evil indices in the names already. If not, use
y1(i, :) = [Data.x0, Data.x10, Data.x20, Data.x30, Data.x40, Data.x50, ...
Data.x60, Data.x70, Data.x80, Data.x90, Data.x100];
Think twice if "y1" is an individual object or if 1 is a hidden index again.

1 commentaire

Daniela N.
Daniela N. le 11 Août 2017
Thanks a lot!! This is exactly what I was searching for.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by