save loop data only if 'if statement' is true
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hey,
I need to make a list of a for loop output if the if statement is true, but feel like I'm missing something basic...
Let's say I have a variable 'Teapot_names' = {'Teapot-1', 'Teapot-2', 'Teapot-3'}
And a list of files I want to iterate through (fileList):
*edited the code to reflect more info
threshold = 0.35
fileList = dir(fullfile(somepath, '*_table.tsv'));
Teapot_names = dir(fullfile(some_path, 'Teapot-*'));
idx = [Teapot_names.isdir]';
Teapot_names = {Teapot_names(idx).name};
for i = 1:numel(fileList)
fullFileName = fullfile(fileList(i).folder, fileList(i).name);
thisTable = readtable(fullFileName,'TreatAsEmpty','n/a','FileType', 'text', 'Delimiter', 'tab');
% here I extract some variables and calculate the variable percent
percent = sum_of_some_variables_thisTable / total_thisTable
if percent > threshold
warning('exceeded threshold by %s', Teapot_names{i});
x{i} = Teapot_names{i};
end
end
that gives the Error: Unable to perform assignment with 0 elements on the right-hand side.
It prints a warning for those iterations for which the if statement is true, but does not print the Teapot_names.
I went through the iterations for each of the files and I know that some if statements are true.
I thought maybe the problem is my understanding of cell arrays, so I tried something that worked in previous scripts (in shorter version):
x = zeros(size(fileList));
for i = numel(fileList)
some_calculations
if some_calculation
x(i) = 1;
end
end
However, that doesnt work either, the variable x stays all zero, while I would expect some of them to have changed to 1.
Can somebody point me in the right direction please =)?
8 comentarios
Voss
el 6 de Feb. de 2022
Whelp, i = [] was my best guess, since it was consistent with all the observations. Of course, I had to speculate that it was happening within a script, since I can't see the whole code.
If you would like us to solve this problem, please share the complete code (and ideally attach the files too and describe the relevant directory structure). Evidently something non-obvious is going on, or else someone would've pointed it out by now.
Respuestas (2)
dpb
el 6 de Feb. de 2022
...
for i=1:numel(fileList)
fullFileName = fullfile(fileList(i).folder, fileList(i).name);
...
That will at least run the loop and address the files in the returned directory structure, assuming there were some that matched the wildcard string.
W/o knowing something about the file structure and seeing the actual rest of the code, we can do no more than guess otherwise, but the above lines as posted would definitely not do what you're expecting them to do...
2 comentarios
dpb
el 6 de Feb. de 2022
Editada: dpb
el 7 de Feb. de 2022
Enough to be able to reproduce the problem, simply put. We can't see your terminal from here, nor can we reproduce what we cannot see--and we don't have data nor the actual complete code to be able to know what happens...and the Crystal Ball Toolbox is still to be released.
Execute
dbstop on error in WHATEVERISYOURCODENAME
and then look at what are the variables at the time. As the error message shows, you DO have an empty element somewhow; how/why we can't tell because of the above.
Substitute your function for "WHATEVERISYOURCODENAME" above, of course.
Voss
el 6 de Feb. de 2022
Editada: Voss
el 6 de Feb. de 2022
Note that if i gets set to the empty array [] somehow (e.g., in a script called from the loop), we get the error:
Teapot_names = {'tp1' 'tp2' 'tp3'};
x = {};
i = [];
warning('exceeded threshold by %s', Teapot_names{i});
try
x{i} = Teapot_names{i};
catch ME
disp(ME.message);
end
So maybe that's what's happening.
And in the other test case, x remains all zeros, like you saw:
x = zeros(1,3);
i = [];
x(i) = 1;
disp(x)
4 comentarios
dpb
el 7 de Feb. de 2022
"WhY" is undoubedly because it is a script and not encapsulated in a function or you're using global variables so that whatever is hanging around in the workspace is there still instead of having a clean workspace inside a function.
Which illustrates why it is so important to post something that is actually reproducible...and a complete example that does reproduce the problem.
Ver también
Categorías
Más información sobre Matrix Indexing 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!