Creating a text file giving all unique parameter values I have looped over in nested loops

2 visualizaciones (últimos 30 días)
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 comentarios
Jan
Jan el 12 de Mzo. de 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?
Distelfink
Distelfink el 13 de Mzo. de 2022
Editada: Distelfink el 13 de Mzo. de 2022
Dear @Walter Roberson, That looks really promising. Thank you very much! Just one question. How do I get the "VarNames" for the results matrix in your loop? Of course I know how I compose the results matrix, i.e. which variables I construct it with. But how can I automatize this process, defining the results matrix with variables and keeping the respective variable names in a cell array, such that the position of the variable name in the cell array is equal to the column number in the results matrix? I think this was the issue I had in the first place.
@Jan Thank you! The reason for creating the text file is that I just want to have a quick overview which parameter values I looped over in a given calibration trial. For each trial my code creates a separate results folder named after the clock time of the initiation, and I just want this text file there to have an overview.
I also noticed that I mixed up the file IDs in the commands for creating the txt file (this is inherited in Walter's comment); I edited this in my original question.

Iniciar sesión para comentar.

Respuestas (1)

Gyan Vaibhav
Gyan Vaibhav el 18 de Nov. de 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.
Hope this helps.
Thanks
Gyan

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by