save loop data only if 'if statement' is true
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
One thing to fix:
for i = numel(fileList)
iterates the loop one time with the value of i equal to numel(fileList). You probably want to say:
for i = 1:numel(fileList)
I'm not sure that is the cause of the problem though. Hard to say without seeing the actual code you are running. You use k to index fileList inside the i loop, for instance. Is this an error in your real code or an error when copy/pasting here? Or what if the calculation for sum_of_some_variables_from_current_table modifies the value of i (or k)? We cannot know.
Alix
el 6 de Feb. de 2022
Alix
el 6 de Feb. de 2022
How modifying the value of i inside a for loop iterating over i can happen (very easily, it turns out):
for i = 1:3
disp(i);
i = 99;
disp(i);
end
So in your case, if sum_of_some_variables_from_current_table is a script that sets the value of i, then that i is the same as in your loop, so if you use it later in the same iteration of the loop, you're using the value set by the script, which is likely incorrect. (This type of bug can be particularly difficult to track down, and that's one reason I don't use scripts at all - except for prototyping/testing the logic of an idea and of course here on MATLAB Answers.) Note that the variable x (or any other variable) could also be set by a script, but if there are no scripts called within your loop, then this shouldn't be a problem.
Alix
el 6 de Feb. de 2022
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
Alix
el 6 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.
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
Alix
el 6 de Feb. de 2022
Voss
el 6 de Feb. de 2022
For what it's worth I added the try/catch in there just to get the code further down to execute, since execution will stop when an error is encountered. But having the try/catch there allows the code to continue to the next iteration of the loop, which may be useful, e.g., does the same error happen next time through the loop? etc.
Alix
el 7 de Feb. de 2022
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.
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!