How can my code, which collects bad data with 'cellfun' and 'try, catch', be improved?
11 次查看(过去 30 天)
显示 更早的评论
My real situatoin is that I have a large number of files. Some I suspect are bad. I want to know the file names of all the bad files. So they will not go to the downstream workflow.
Here is a toy example I write to show the problem I have. I only want to keep the bad data. So from cellfun's output, I must remove the good data (coded as '0'). Is there any better way to do the whole thing? I appreciate any suggestion you have.
list = {'a', 'bc', 'defg'};
T = cellfun(@(x) func(x), list, 'UniformOutput',false)
function out = func(x)
try
x(4); % This would create error for a character vecctor shorter than 4.
out = 0;
catch ME
warning("--- error caused by bad data ---.\n")
out = x; % This is to collect the bad data.
disp(x)
disp(ME)
end
end
2 个评论
dpb
2022-9-11
I'd revert to asking what defines a "bad" file vis a vis a "good" one...and what is the input format for the files?
采纳的回答
Paul
2022-9-11
Have func() return a logical with true indicating the file is bad and false indicating the file is good. In this case we'd have
list = {'a', 'bc', 'defg'};
T = cellfun(@(x) func(x), list)
% remove the good data
list(T)
The try/catch scheme may not be necessary depending on what the criteria really are for determing the goodness of a file and how those criteria can be tested.
function out = func(x)
out = false;
try
x(4); % This would create error for a character vecctor shorter than 4.
catch ME
warning("--- error caused by bad data ---.\n")
out = true; % This is to collect the bad data.
disp(x)
disp(ME)
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!