What does the input function do in MATLAB / RunMat?
input(prompt) displays prompt, waits for the user to type a line of text, and then returns the
evaluated value. When you pass 's' as the second argument, input skips numeric parsing and
returns the raw text as a character row vector. RunMat mirrors MATLAB’s single-line prompt/response
flow so scripts that rely on interactive questions continue to work.
Behaviour in RunMat
- Prompts accept character row vectors and string scalars. When omitted, RunMat uses
"Input: ". - The function always blocks until the host supplies a line of text (via the REPL, CLI pipe,
or wasm bindings).
stdinRequestedevents surface the outstanding prompt to JS hosts so they can resume later. - When
's'(or"s") is provided, the return value is a character array containing exactly what the user typed (without the trailing newline). - Without
's', RunMat forwards the response through the same numeric parser that backsstr2double. This covers scalar numbers, MATLAB-style vector/matrix literals,Inf,NaN, and complex tokens. Arbitrary expressions (for example1+rand()) are not evaluated automatically—read the text with's'and pass it toevalonce that builtin lands. - Empty responses (just pressing Enter) return an empty
[]double, consistent with MATLAB. - When the numeric parser rejects the text, RunMat raises
MATLAB:input:InvalidNumericExpressionso callers can present a friendly retry loop.
Examples
value = input("Enter a scalar: ");
name = input("Your name? ", "s");
fprintf("Hello, %s!\n", name);
vec = input("Enter a row vector like [1 2 3]: ");
raw = input("Type anything and I will echo it back: ", "s");
disp("You typed: " + raw)
FAQ
Does RunMat evaluate arbitrary MATLAB expressions?
Not yet. The numeric branch reuses the str2double parser, which supports MATLAB numeric literals
(scalars, vectors, matrices, Inf, NaN, complex pairs) but does not run arbitrary code. When you
need to evaluate expressions such as 1+sin(pi/4), capture the text via input(..., 's') and pass
it to eval in a follow-up release.
What happens if the user just presses Enter?
RunMat returns the empty double [], matching MATLAB’s input behaviour.
Can I call input inside GPU workloads?
Yes. Prompts always flow through the host console/UI. GPU-resident prompt arguments are gathered once before display, and the response itself is always a host value.