Filter löschen
Filter löschen

Determine Path of Mex S function from the C/C++ mex code

6 Ansichten (letzte 30 Tage)
Ben
Ben am 9 Mai 2013
Beantwortet: Graham Fletcher am 17 Feb. 2020
I have a C++ level 2 S function that I am compiling with mex. This s function loads data from external files that are located relative to the s function file itself. Say, the data is located at "..\data" relative to the s function file itself (say it is myFunction.mexw32). Is is it possible from my C++ code to determine the path the S function so that I can then determine the location of my data files? I know this is possible in a script using the mfilename('fullpath') method, but I can't find how to do it in an S function.
I really really don't want to hard code the path to the data as that will cause other issues. If I leave it relative it looks it up relative to MATLAB's current directory, not the directory of the S function (which is located elsewhere on MATLAB's path).
Thanks for any help!

Akzeptierte Antwort

Kaustubha Govind
Kaustubha Govind am 9 Mai 2013
In MATLAB, you can look up the full path to any given file using the MATLAB function which. You can call this from your S-function using mexCallMATLAB, with the S-function name passed in as the input argument. Note that the S-function name is stored in the macro S_FUNCTION_NAME which is defined at the top of every S-function.

Weitere Antworten (2)

Graham Fletcher
Graham Fletcher am 17 Feb. 2020
I realise this is quite old, but this can be achieved without needing to call back into Matlab. On Windows, I use
GetModuleHandleEx
and
GetModuleFileName
in conjunction to get the path to the mexw64.See this StackOverflow post for a fuller description.

Ben
Ben am 10 Mai 2013
A little bit roundabout, but it works great. Thanks!

Kategorien

Mehr zu C Shared Library Integration finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by