What does the which function do in MATLAB / RunMat?
which name reports exactly which entity RunMat will call when you invoke name. It follows MATLAB's search order:
- Variables in the current workspace
- Builtin functions
- Class folders and classdef files
- MATLAB files on the RunMat path (
.m,.mlx, etc.) - Folders
The builtin accepts the same flags as MATLAB: -all, -builtin, -var, and -file.
How does the which function behave in MATLAB / RunMat?
- Names can be supplied as character vectors or string scalars. Calling
whichwith no name raiseswhich: not enough input arguments. which namewithout options returns the first match respecting MATLAB's precedence rules.which(name, "-all")(orwhich("-all", name)) returns a cell array with every match on the search path, in discovery order, without duplicates.which(..., "-builtin")restricts the search to builtin functions."-var"restricts to workspace variables, and"-file"restricts the search to files, classes, and folders.- Package-qualified names like
pkg.funcautomatically map to+pkgfolders. Class lookups recognise both@ClassNamefolders and.mfiles containingclassdef. - Relative paths are resolved against the current working directory. Absolute paths and paths beginning with
~or drive letters are honoured directly.
which Function GPU Execution Behaviour
which performs string parsing and filesystem inspection on the host CPU. If you pass GPU-resident strings (for example, gpuArray("sin")), RunMat gathers them automatically before evaluating the request. Results are always host-resident character arrays or cell arrays. Acceleration providers do not implement kernels for this builtin.
GPU residency in RunMat (Do I need gpuArray?)
No. which gathers GPU arguments implicitly and never produces device-resident output. There is no benefit in moving strings to the GPU before calling which.
Examples of using the which function in MATLAB / RunMat
Finding a built-in function's implementation
which("sin")
Expected output:
built-in (RunMat builtin: sin)
Checking if a workspace variable shadows a builtin
answer = 42;
which("answer")
Expected output:
'answer' is a variable.
Listing all matches on the path
which("sum", "-all")
Expected output (example):
{
[1,1] = built-in (RunMat builtin: sum)
[2,1] = /Users/alex/runmat/stdlib/sum.m
}
Locating a script or function file
which("helpers/process_data")
Expected output:
/Users/alex/projects/runmat/helpers/process_data.m
Restricting the search to variables
which("-var", "velocity")
Expected output (if the variable exists):
'velocity' is a variable.
Restricting the search to files
which("fft", "-file")
Expected output (example):
/Users/alex/runmat/overrides/fft.m
FAQ
- What happens when nothing is found?
whichreturns the character vector'<name>' not found.just like MATLAB. - Are method lookups supported? Methods defined via
@Classfolders orclassdeffiles are discovered through the class search. Package-qualified methods are supported. - Does
whichcanonicalise paths? Yes. RunMat reports canonical absolute paths where possible; when canonicalisation fails, the original path is returned. - Can I combine
-allwith other options? Yes. For example,which("plot", "-all", "-file")lists every file-based implementation without reporting builtins or variables. - Does the search respect
RUNMAT_PATH/MATLABPATH? Yes. The directory list mirrors the logic used by other REPL filesystem builtins. - What about Simulink models or Java classes? File-based matches with supported extensions (
.slx,.mdl,.class, etc.) are reported when present on the path. - Are duplicate results filtered? Yes. The first occurrence of each unique path is returned.
- Is the lookup case sensitive? No. Matching is case-insensitive on all platforms, following MATLAB semantics.
- Does
whichgather GPU values? Yes. GPU-resident arguments are automatically gathered before the search begins.
See Also
Source & Feedback
- Source:
crates/runmat-runtime/src/builtins/introspection/which.rs - Found an issue? Open a GitHub ticket with a minimal reproduction.