How to use a relative path for NET.addAssembly

My matlab code is in D:\CERDEC\liveFeed, but I find I have to use a full path:
function setup(this)
this.asm = NET.addAssembly('D:\CERDEC\liveFeed\bin\Debug\liveFeed.dll');
this.feed = liveFeed.Feeder('me');
end
I get an error when I try to use:
this.asm = NET.addAssembly('.\bin\Debug\liveFeed.dll');

 Accepted Answer

Well, you have no guarantee that the current matlab working directory is 'D:\CERDEC\liveFeed\'. And honestly, it is much better to always work with full paths. But if you really want to use a relative path, you can use pwd to query the current working directory, so:
this.asm = NET.addAssembly(fullfile(pwd, '.\bin\Debug\liveFeed.dll'));

5 Comments

In our case, I am creating a zip package with a well defined hiearchy of folders, and the matlab will always be in the same relative path to the DLL, and I want users on a number of computers to use this, regardless of what drive they put it on (i.e. other that D:)
In that case, you should be using mfilename to get the path of the class file regardless of the current directory (which may not be the same at all as where the class m file resides), so:
this.asm = NET.addAssembly(fullfile(fileparts(mfilename('fullpath')), '.\bin\Debug\liveFeed.dll'));
>> fullfile(fileparts(mfilename('fullpath')), '.\bin\Debug\liveFeed.dll ')
ans =
'.\bin\Debug\liveFeed.dll '
never-mind, I see I have to run this inside the class.m file.
Thanks for all the speedy answers.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Tags

Asked:

on 19 Oct 2017

Edited:

on 24 Oct 2017

Community Treasure Hunt

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

Start Hunting!