- cell2table function: https://www.mathworks.com/help/matlab/ref/cell2table.html
- writetable function: https://www.mathworks.com/help/matlab/ref/writetable.html
Store results from multiple loops and parameter values
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I have the following multiple loop
for i=1:length(thetas)
theta = thetas(i); % Utility function
for j=1:length(rhos)
rho = rhos(j);
for ii=1:length(gammas)
gamma = gammas(ii);
[kss]=equilibirum(debt);
end
end
end
where in each step I essentially change some parameter values to get different values for the column vector
kss
for example the vector of parameters I am looping over are:
% define cases
thetas = [1, 1.5];
rhos = [0, 0.99, 2];
gammas = [-1,0,0.76, 0.9, 1] ;
Now, I want to remember (or store) for which combination of parameters i get the values for `kss' .
How could I do this matlab in some easy to understand and easy to export (e.g. in excel) way?
0 commentaires
Réponses (1)
Ronit
le 30 Mai 2025
Hello,
You can store the results in a MATLAB table which is both easy to understand and easy to export to a excel sheet using the "writetable" function. Each row of the table corresponds to a unique combination of "theta", "rho", and "gamma", along with the resulting "kss".
Refer to the following code for better understanding:
thetas = [1, 1.5];
rhos = [0, 0.99, 2];
gammas = [-1, 0, 0.76, 0.9, 1];
results = {};
row = 1;
for i = 1:length(thetas)
theta = thetas(i);
for j = 1:length(rhos)
rho = rhos(j);
for ii = 1:length(gammas)
gamma = gammas(ii);
kss = equilibirum(debt);
% Store parameters and kss as a row
results{row, 1} = theta;
results{row, 2} = rho;
results{row, 3} = gamma;
results{row, 4} = kss;
row = row + 1;
end
end
end
T = cell2table(results, 'VariableNames', {'Theta', 'Rho', 'Gamma', 'KSS'});
% Export to Excel
writetable(T, 'kss_results.xlsx');
For more details on the above functions, please refer to the following documentation pages:
I hope this resolves your query.
Thanks
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!