MATLAB: How to access Matlab search paths from mex file

mexs-functionsearch pathsimulinksimulink coder

I have som legacy C-code that runs as S-function within a Simulink model. Within this C-code, a text file is read using fopen. As long as the text file is located in the current Matlab directory, the file can be read successfully during simulation.
If the text file is located in some other directory which is included in pathdef.m but not the current directory, it won't be found.
Is there a way to search for the file same as 'which' would do ? All within the S-function.
Thanks in advance.

Best Answer

  • The function mexCallMATLAB allows to run Matlab commands from the MEX file. Use this to call Matlab's which.
    But I'd recommend to avoid such smart searching, because the result depends on details, the user cannot control, e.g. the order of folders inside the path and the current directory. Better rely on absolute path names to access files.