how to batch save and load loop variable
5 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 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?
Answers (2)
KALYAN ACHARJYA
on 19 Jun 2022
Edited: KALYAN ACHARJYA
on 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.
0 Comments
Voss
on 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);
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);
There is no need to make several variables; use one variable (matrix, cell array, etc.) and index into that.
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!