how to batch save and load loop variable

6 Ansichten (letzte 30 Tage)
peter huang
peter huang am 19 Jun. 2022
Kommentiert: Stephen23 am 19 Jun. 2022
Hello, everyone
I want to access the result of each calculation in the loop calculation I would like to ask if MATLAB has any instructions for batch storage and batch input of variables? and I want to make each stored variable have a different name
ex code
for i = 1:3
a = rand(1,10)
end
a =
0.5817 0.5337 0.8386 0.3502 0.3441 0.9860 0.8664 0.5777 0.2776 0.2758
a =
0.2768 0.5556 0.1333 0.3556 0.8931 0.9569 0.0273 0.2038 0.2547 0.7161
a =
0.2658 0.2586 0.4210 0.5831 0.6447 0.0349 0.2437 0.3171 0.3266 0.4875
I want to convert each generated a into a mat file and be able to read these three sets of a to matlab
Because using normal access will be overwritten because the name is all a
  1 Kommentar
Stephen23
Stephen23 am 19 Jun. 2022
"Because using normal access will be overwritten because the name is all a"
Only if you write code that overwrites that variable.
"I want to make each stored variable have a different name"
That would be a very complex and inefficient approach, whereas you could just use simple and efficient indexing.
What is stopping you from using basic indexing?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

KALYAN ACHARJYA
KALYAN ACHARJYA am 19 Jun. 2022
Bearbeitet: KALYAN ACHARJYA am 19 Jun. 2022
#Cell Array cell
a=cell(1,3);
for i = 1:3
a{i}=rand(1,10)
end
Once strore in the cell array, you can do/access data in easiest & efficient way.

Voss
Voss am 19 Jun. 2022
How about making a a matrix?
a = zeros(3,10);
for i = 1:3
a(i,:) = rand(1,10);
end
disp(a);
0.6037 0.8371 0.3572 0.1454 0.0825 0.5461 0.5211 0.5553 0.2386 0.9297 0.6848 0.8260 0.3338 0.5792 0.9571 0.5220 0.1013 0.0640 0.6952 0.6487 0.6763 0.6561 0.1229 0.5876 0.1097 0.0735 0.6933 0.0960 0.8610 0.0459
Or, if each iteration produces a vector of a different length, make a a cell array:
a = cell(3,1);
for i = 1:3
a{i} = rand(1,randi(10));
end
disp(a);
{[ 0.0980 0.9935]} {[0.9495 0.0781 0.8634 0.3343 0.1644 0.7231 0.6781 0.6991 0.4505]} {[ 0.9395 0.4197 0.9975 0.2326 0.2881 0.4256 0.7652]}
There is no need to make several variables; use one variable (matrix, cell array, etc.) and index into that.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by