0

I'm trying to make a .m file that uses a .dll file and I want to be able to pass my script to other people without error. The only problem is that matlab always searches starting at the matlabroot or some drive if you specify. The location of the folder containing this project is going to vary depending on the user. So I can't simply use the function:

loadlibrary("C:\Users\Public\Documents\projectFolder\file.dll", "C:\Users\Public\Documents\projectFolder\file.h")

in my .m file assuming that every user has the project folder in "C:\Users\Public\Documents".

I'm trying to see if there's a way for matlab to just know where the .m file is and start at that path, then maybe I could set the code up like this:

path = [some code which finds the path of .m file];

loadlibrary(strcat(path, 'file.dll'), strcat(path, 'file.h'));

Thanks

craigim
  • 3,884
  • 1
  • 22
  • 42

1 Answers1

0

MATLAB requires any .m file to be in its search path before it will run it (I don't think that's strictly true, but it will complain mightily if it isn't). Users can add and/or subtract directories from their path pretty easily, and so it's a safe assumption that if the user has installed your script, they've put it somewhere in their MATLAB path. If they haven't, and they try to run it, MATLAB will pop up a dialog warning that the script is not in the path and asking if they would like to temporarily add it.

As to how to find it and how to use that information, you'd do something like this:

scriptLocation = mfilename('fullpath');
[directory,name,extension] = fileparts(scriptLocation);
dllLocation = fullfile(directory,[name '.dll']);
headerLocation = fullfile(directory,[name '.h]);

loadlibrary(dllLocation, headerLocation);
craigim
  • 3,884
  • 1
  • 22
  • 42
  • Thank you for the answer, though the part about a .m file having to be in the search path isn't true and mine never are (though they really should be). I was hoping that there would be a solution to this but I guess that I'll just have to hope the user has enough common sense to make assumptions. – Absent Lung Jan 02 '15 at 16:59
  • I don't have MATLAB in front of me at the moment to test it, but the `mfilename` command may still be able to find the current script even if it isn't in the path. In which case this would work in all situations. – craigim Jan 02 '15 at 17:02