How to store matrices in a cell from a loop?
22 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Sambit Supriya Dash
2021 年 7 月 15 日
Could you put an example as the code, and should exactly be the output, so that it would be more clear to address.
採用された回答
Stephen23
2021 年 7 月 16 日
編集済み: Stephen23
2021 年 7 月 16 日
Rather than lots of separate variables, use a comma-separated list to store the function outputs:
then transfer the required data to preallocated output matrices. Something like this should get you started:
itr = 55;
tmp = cell(1,22);
out = tmp;
out(:) = {nan(24,itr)};
for ii = 1:itr
[tmp{:}] = gams('Model.gms');
for jj = 1:numel(tmp)
out{jj}(:,ii) = tmp{jj}.val(:,2);
end
end
Checking the output (note that it is simpler to collect all 22(?) outputs, not just the 16 that you need. If you really can only collect those 16 outputs, add indexing as appropriate):
out
out{1} % first cell
"I need to add a header to each cell to identify the variables."
Cell arrays do not have headers. If you want a header then you should use a structure or a table, for example:
fld = {'Cost', 'GridCosts', 'IntCosts', 'BatCosts', 'EVCosts', 'RegCosts', 'Ppv', 'Pev', 'Pbat', 'Pinv', 'Pgrid', 'Ebat', 'Eev', 'Pev_up', 'Pev_dwn', 'Pbat_up', 'Pbat_dwn', 'Ppv_dwn', 'P_reg_up', 'P_reg_dwn', 'SoC_bat', 'SoC_ev'};
str = cell2struct(out,fld,2)
str.Cost % checking
Fake GAMS function:
function varargout = gams(s)
persistent m
if isempty(m); m=1e3; else m=m+1e3; end
fun = @(n) struct('val',(1:24).'+m+n*[0,100]);
varargout = arrayfun(fun,1:22,'Uni',0);
end
その他の回答 (1 件)
ANKUR KUMAR
2021 年 7 月 16 日
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)
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!