How to access Matlab search paths from mex file ?

2 views (last 30 days)
Hi,
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.

Accepted Answer

Jan
Jan on 27 Mar 2014
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.
  1 Comment
Frieder
Frieder on 28 Mar 2014
thanks for your reply. It works with mexCallMATLAB but I am now having trouble converting the mxArray back to char because the returned path contains backslashes. When I use mxArrayToString the first part is lost. How can I replace the '\' within the mxArray ?
Here's an example, searching for a 'search.txt' file
if true
% code
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *mylhs;
mxArray *myrhs = mxCreateString("search.txt");
char *fullstr;
mexCallMATLAB(0, NULL, 1, &myrhs, "disp");
mexCallMATLAB(1, mylhs, 1, &myrhs, "which");
mexCallMATLAB(0, NULL, 1, mylhs, "disp");
fullstr = mxArrayToString(mylhs);
mexPrintf(fullstr);
mxDestroyArray(myrhs);
}
end

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Report Generator 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!