I would say your general approach is probably about the best that you could hope for. You need to determine which files exist, and you need to determine which ones are on the path.
You could make some efficiency improvements. For example you could unique() the folder information before doing the contains(). You could use ismember() instead of contains().
Unfortunately, dir() does not itself recognize using '[1-3]' so you cannot combine the restriction with the dir()
Depending on your operating system, the search might potentially be more efficient if you system() out to a system path search. For example on MacOS or Linux, you might be able to use
[status, results] = system('find ~/code -name \*.h -print')
I do not have enough experience with WIndows do know if you can do something like
[status, results] = system('dir /s *.h')
but in a way that restricts to C:\code as the starting directory.