2

Can 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)?

Matthew Simoneau
  • 6,199
  • 6
  • 35
  • 46
shahar_m
  • 3,461
  • 5
  • 41
  • 61

4 Answers4

3

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
Community
  • 1
  • 1
Matthew Simoneau
  • 6,199
  • 6
  • 35
  • 46
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.

Community
  • 1
  • 1
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • @richie-cotton: I'm using windows xp, so I get 'which' is not recognized as an internal or external command, operable program or batch file. – shahar_m Apr 28 '11 at 13:32
0

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.

Egon
  • 4,757
  • 1
  • 23
  • 38
0

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.

abcd
  • 41,765
  • 7
  • 81
  • 98
  • Nice way to circumvent the limits of Windows :-). Just a few additions: on Windows it's a backslash (`\ `), but I think MATLAB can handle a normal slash as well on both Windows and Unix as a path separator (you can also take a look at `filesep` for the correct one) On the other hand, by adding your whole drive to the MATLAB path, my biggest fear is that the performance of MATLAB will suffer a lot (especially when you call functions that do not exist). – Egon Apr 28 '11 at 15:24
  • @Egon: I know... I was just pointing out to the OP that making MATLAB do an OS' job is kinda unreasonable. – abcd Apr 28 '11 at 15:28
  • I know you know. But I wanted to point it out very clearly to the OP that you might not only end up with a performance hit once by running that code but possibly every time you try to access a file (e.g. .m-files). – Egon Apr 28 '11 at 15:37
  • @Egon: You're right, I should've mentioned that to the OP. Thanks for doing that :) – abcd Apr 28 '11 at 15:48