Creating a text file giving all unique parameter values I have looped over in nested loops
2 views (last 30 days)
Show older comments
Dear matlab forum,
I am running a model and trying out various parameter specifications using nested loops. Something like this:
loopnumber=1;
for l=[1 2 3]
for m=[4 5 6]
for n=[7 8 9]
x=l+m+n;
results(loopnumber,:)=[l m n x];
loopnumber=loopnumber+1;
end
end
end
As you see I save my results in a matrix "results" including the respective parameter choice for every parameter combination.
After this, I want to create a txt file giving an overview of the values I have used for every parameter I have looped over. Something like this:
fid=fopen(chosenparavals.txt,'w');
fprintf(fid,'\r\n l \r\n');
fprintf(fid,'%12.3f',unique(results(:,1)));
fprintf(fid,'\r\n m \r\n');
fprintf(fid,'%12.3f',unique(results(:,2)));
fprintf(fid,'\r\n n \r\n');
fprintf(fid,'%12.3f',unique(results(:,3)));
fclose(fid);
In reality I have more parameters, and I am wondering how I can do this more nicely. In particular, the above code structure is quite likely to produce errors. For example, if I add another parameter p that I loop over and whose respective values I want to save in my results matrix for every iteration, I will possibly have to adapt many indices in the creation of the txt file.
Could I somehow connect the variable names (l m n) and their unique values to create chosenparavals.txt in a loop? I suspect it has to do with a clever usage of string and numeric values - but I can't find the solution.
I would be grateful for any tips. Thanks a lot in advance!
5 Comments
Jan
on 12 Mar 2022
"how to efficiently create a text file summing up the values for each parameter that I have looped over" - I do not understand, what you are asking for. What is the reason to create a text file?
Answers (1)
Gyan Vaibhav
on 18 Nov 2023
Hi Distelfink,
I understand that you are trying to create a text file which logs the values of every parameter that were used while running your model.
This can be achieved by storing the parameter names and their values using cell arrays. Further, “ndgrid” can be used to generate all possible combinations and then are stored in the “paramCombinations” cell array.
It can be done as follows:
% Define parameter names and their values
paramNames = {'l', 'm', 'n', 'star'};
paramValues = {[1 2 3], [4 5 6], [7 8 9],};
% Initialize results matrix
numParams = numel(paramNames);
numCombinations = prod(cellfun(@numel, paramValues));
results = zeros(numCombinations, numParams + 1); % +1 for the computed value
% Generate parameter combinations and compute results
paramCombinations = cell(1, numParams);
for i = 1:numParams
paramCombinations{i} = paramValues{i};
end
[paramCombinations{:}] = ndgrid(paramCombinations{:});
paramCombinations = cellfun(@(x) x(:), paramCombinations, 'UniformOutput', false);
paramCombinations = cat(2, paramCombinations{:});
for i = 1:numCombinations
params = paramCombinations(i, :);
x = sum(params);
results(i, :) = [params, x];
end
% Write parameter values to txt file
fid = fopen('chosenparavals.txt', 'w');
for i = 1:numParams
fprintf(fid, '\r\n %s \r\n', paramNames{i});
fprintf(fid, '%12.3f', unique(results(:, i)));
end
fclose(fid);
This approach makes it much easier to add or remove parameters, without having to modify the code that enables to write the parameter values to the “TXT” file.
Here are the links to the “ndgrid” and “Cell Arrays” documentations, to learn more about them.
- ndgrid : https://www.mathworks.com/help/matlab/ref/ndgrid.html
- Cell Arrays: https://www.mathworks.com/help/matlab/cell-arrays.html
Hope this helps.
Thanks
Gyan
0 Comments
See Also
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!