Unrecognized function or variable
4 views (last 30 days)
Show older comments
function ButtonPushed(app, event)
If I do something like this:
TF = contains({files_in_folder.name}, short_serials(2));
the result is:
1 0 0 0 0 0 0 0 0 0
Once I get an index with a value of 1, I want to load a file. In this example, {files_in_folder(1).name} = 1, so I want to load that file name in {files_in_folder(1).name}.
I tried a for loop but get the following error:
app.SelectTestID.Items = short_serials;
TF = contains(files_in_folder.name, app.SelectTestID.Value);
counter = 0;
for i = files_in_folder
counter = counter + 1;
if TF(counter) == 1
VideoFile = files_in_folder(counter).name;
end
end
Error using contains
Incorrect number of arguments. Each parameter name must be followed by
a corresponding value.
If I instead use:
TF = contains({files_in_folder.name}, app.SelectTestID.Value);
Then I get this error:
Unrecognized function or variable 'VideoFile'.
at the following line of code:
vidObj = VideoReader(VideoFile);
1 Comment
Image Analyst
on 29 Jul 2022
This is just basic debugging
What is files_in_folder.name, and short_serials(2), and what is app.SelectTestID.Value? Are they a string? Cell array? Number/double? If they're an array, how many elements do they have.
Please invest a few minutes here:
Answers (1)
Voss
on 29 Jul 2022
This is correct:
TF = contains({files_in_folder.name}, app.SelectTestID.Value);
And the reason you get the error that VideoFile is undefined is because this line is never executed:
VideoFile = files_in_folder(counter).name;
The reason that line is never executed is because the condition TF(counter) == 1 is never true.
The reason the condition TF(counter) == 1 is never true is (I suspect) because files_in_folder is a column vector, so the for loop executes only once (since for operates over the columns of the array it is given) and TF(1) is not 1.
To fix that, you can use a row vector in the for loop:
for i = files_in_folder.' % transpose to make a row vector
% same as what you have now
end
But a simpler construction would be:
for counter = 1:numel(files_in_folder)
if TF(counter) == 1
VideoFile = files_in_folder(counter).name;
end
end
Then, after the loop is done, VideoFile will be the name of the last file for which TF is 1 (and VideoFile would still be undefined if no element of TF is 1).
If you want the first file instead, then break as soon as TF(counter) is 1:
for counter = 1:numel(files_in_folder)
if TF(counter) == 1
VideoFile = files_in_folder(counter).name;
break
end
end
And, throughout, since TF is a logical array, you don't need to compare its elements to 1. You can merely say:
if TF(counter) % == 1 not necessary
end
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!