How can I index where I found the contents of strfind?

5 views (last 30 days)
I have a structure "MyStructure" that lists six contents of a folder. MyStructure's first column is 'name'. I have been using strfind (MyStructure.name) to locate a specific one of the six contents . Let's say it is in the 4th row. How can I have matlab tell me in which row this file is located?

Answers (1)

Geoff Hayes
Geoff Hayes on 4 May 2019
Nicholas - perhaps something liek the following could be used if the MyStructure is similar to yours.
MyStructure = [];
for k = 1:5
MyStructure(k).name = sprintf('filename_%d.txt', k);
end
strMatch = arrayfun(@(x)strcmp(x.name, 'filename_4.txt'), MyStructure)
We use arrayfun to a apply an anonymous function to each struct in your array. We compare the name of each to (in this case) the fourth file name. The strMatch variable is a logical array of ones and zeros. If there is a one, then that will tell you which index (and so row) the match is located in.

Categories

Find more on Characters and Strings 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!