save loop data only if 'if statement' is true

3 views (last 30 days)
Alix
Alix on 6 Feb 2022
Commented: dpb on 7 Feb 2022
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 Comments
Alix
Alix on 6 Feb 2022
ah okay like that, thx.
I am not using a script, and i'm not calling an empty array i=[] anywhere.
the sum_of_some_variables_from_current_table is just the sum of two columns in the table (thisTable).
The total_thisTable is the number of observations in the table (hight(thisTable)
Voss
Voss on 6 Feb 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.

Sign in to comment.

Answers (2)

dpb
dpb on 6 Feb 2022
As @Benjamin pointed out, for starters fix the loop construct and indexing...
...
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 Comments
Alix
Alix on 6 Feb 2022
as I replied and changed in the code i posted, it was a typo.
the rest of the code is a bit long, but as I just replied to @Benjamin, the variable 'percent' is calculated correctly for each iteration, and the if statement works fine I believe as the warning message is being spit out for the iterations that meat the statement. The only thing that doesn't happen is that the Teapot_name is not inserted at the end of the warning message, or added to x.
What other information would you need?
dpb
dpb on 6 Feb 2022
Edited: dpb on 7 Feb 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.

Sign in to comment.


Voss
Voss on 6 Feb 2022
Edited: Voss on 6 Feb 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});
Warning: exceeded threshold by %s
try
x{i} = Teapot_names{i};
catch ME
disp(ME.message);
end
Unable to perform assignment with 0 elements on the right-hand side.
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)
0 0 0
  4 Comments
Alix
Alix on 7 Feb 2022
Thank you so much for all your time and help @Benjamin @dpb. I have no idea why but after restarting my laptop this morning and clearing out all the workspace in matlab it worked. ...Really sorry to have put you through all this, I did learn a lot from your replies and examples though! Thank you for being so kind and responsive =)
dpb
dpb on 7 Feb 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.

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!