开发者

Finding the path of installed program from MATLAB?

C开发者_开发知识库an I find out from the MATLAB command line what is the installation path of specific program? Or can I find the path for a registered program (Windows reg equivalent)?


This won't be 100% reliable, but this will get the right answer most of the time:

function p = findOnSystemPath(f)
p = '';
path = getenv('path');
dirs = regexp(path,pathsep,'split');
for iDirs = 1:numel(dirs)
    tp = fullfile(dirs{iDirs},f);
    if exist(p,'file')
        p = tp;
        break
    end
end

Sample usage:

>> findOnSystemPath('runemacs.exe')

ans =

C:\Program Files (x86)\emacs\bin\runemacs.exe

Depending on your OS, you might be able to get this information from the system directly:

which is available on Unix systems and Windows systems with Cygwin installed:

>> [~,p] = system(sprintf('which "%s"',f))

p =

C:/Program Files (x86)/emacs-mw-a/bin/runemacs.exe

where is available on Windows 2003 and later:

>> [~,p] = system(sprintf('where "%s"',f))

p =

C:\Program Files (x86)\emacs-mw-a\bin\runemacs.exe

And in some cases, you can pull this information from the registry using winqueryreg, for example:

>> notepadEdit = winqueryreg('HKEY_CLASSES_ROOT','Applications\notepad.exe\shell\edit\command')

notepadEdit =

C:\Windows\system32\NOTEPAD.EXE %1


Call the DOS/bash command which, e.g.,

!which matlab
!which notepad

(Or use system instead of the !.)

EDIT: It seems that there isn't a direct equivalent in Windows. I had cygwin installed on the (Win XP) machine I tried it on, and the command succeeded. Alternatively, take a look at these answers on stackoverflow and superuser.


This depends on what you know about the OS and what properties your program has.

On Linux I usually do something like:

[error, path] = system(sprintf('which "%s"',programName));

It doesn't look pretty and it's far from portable (I suppose it will not work on Windows, perhaps only if you install Cygwin or something similar). It's far easier in Unix since most executables are accessible from the "path" (the environment variable "path"), while in Windows most executables are either stored in the Windows directory (which is in the default path, so they are found) or in a Program Files directory which is not as far as I recall.

Error = 0 when the program is found and path then obviously contains the path to the executable.

For Windows I guess you can search all directories for the program, but that might be somewhat tedious.


MATLAB is not really designed to be used as a tool to search files anywhere on the drive. That's a task best left to the OS, and what Egon suggested is what you should be doing. Simply replace which with the equivalent in DOS (you should know this already, else just ask another question in the MS-DOS/Windows tag. It probably has already been answered.).

If you are really hell bent on using MATLAB to search the drive, then you can do the following

addpath(genpath('C:\')); %#' I am not sure which way the slash is
which filename

Beware, the first step will take a while.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜