MATLAB Answers

0

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

Asked by Nicholas van den Berg on 4 May 2019
Latest activity Answered by Geoff Hayes
on 4 May 2019
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?

  0 Comments

Sign in to comment.

1 Answer

Answer by 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.

  0 Comments

Sign in to comment.