I think if `MATLAB_ROOT_DIR_` is empty, then this whole block starting from `#registry-hacking` does nothing. So perhaps move the first condition here to enclose the whole block? You still want to avoid setting `MATLAB_ROOT_DIR_` twice in the foreach loop, I guess, but you can just use `break` for that.

Also, can you explain what the second condition means?