How to store matrices in a cell from a loop?
Show older comments
I am running an external software (GAMS) to do some optimization where I call GAMS in a loop and get back 16 variables (matrices) that I want to store in a cell in every iteration from the loop. I have trouble doing so without delcaring the variables in the workspace first. See bellow what I did. Thanks.
for k = 1:55
% Some calculations in every iteration (k)
% Call GAMS model in every iteration (k) and get 16 variables
[Var1 Var2 Var3 ...] = gams('My_model.gms')
% This is what I do now but want to modify
Var1(:,k) = Var1.val(:,2);
Var2(:,k) = Var2.val(:,2);
Var3(:,k) = Var3.val(:,2);
end
Instead of declaring every variable in the workspace, which are matrices (24x55), I want to store the variables in a cell array: My_cell = {1,16}, where each cell is the matrix (24x55) that results from the iterations and I need to add a header to each cell to identify the variables.
The difficulty I have is that every matrix is written in sequence (k) e.g, k=1 (24x1), k=2 (24x2) ... k=55 (24x55) and at the same time I need to fill the cells of My_cell = {1,16} in the loop. I have tried with a nested loop without success.
2 Comments
Sambit Supriya Dash
on 15 Jul 2021
Could you put an example as the code, and should exactly be the output, so that it would be more clear to address.
Pedro Leal
on 15 Jul 2021
Accepted Answer
More Answers (1)
I am taking random data to show how you can store the output in a cell array using loop.
for index=1:16
output{index}=randi(100,25,55);
end
output
You can do even without using loop too.
arrayfun(@(x) randi(100,25,55), 1:16, 'uni', 0)
1 Comment
Pedro Leal
on 19 Jul 2021
Categories
Find more on Logical 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!