Creating a text file giving all unique parameter values I have looped over in nested loops
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2022-3-12
"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?
回答(1 个)
Gyan Vaibhav
2023-11-18
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!