input — Prompt users for interactive input in MATLAB and RunMat.
input(prompt) displays prompt, reads one line from the user, and returns the evaluated value. Passing 's' as the second argument returns raw text instead of numeric evaluation, following MATLAB semantics.
Syntax
value = input()
value = input(prompt)
value = input(prompt, stringFlag)
value = input(stringFlag, prompt)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
prompt | Any | Yes | — | Prompt text shown to the user. |
stringFlag | StringScalar | Yes | — | Set to 's' to return the raw input text. |
Returns
| Name | Type | Description |
|---|---|---|
value | Any | Parsed scalar/matrix value, or raw text when using string mode. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:input:TooManyInputs | More than two input arguments are passed to input. | input: too many inputs |
RunMat:input:InvalidStringFlag | The string mode flag is not a scalar string/char 's'. | input: invalid string flag |
RunMat:input:PromptMustBeRowVector | Prompt char array is not 1-by-N. | input: prompt must be a row vector |
RunMat:input:PromptMustBeScalarString | Prompt string array is not scalar. | input: prompt must be a scalar string |
RunMat:input:InvalidPromptType | Prompt is not a string scalar or row char vector. | input: invalid prompt type |
RunMat:input:InteractionFailed | Interactive prompt callback fails. | input: interaction failed |
RunMat:input:EvalFailed | Expression evaluation hook rejects the input expression. | input: invalid expression |
RunMat:input:InvalidNumericExpression | Numeric fallback parser rejects the input expression. | input: invalid numeric expression |
How input works
- 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). In wasm hosts, prompts are delivered to the installed async input handler and execution continues after the handler resolves.
- 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
RunMat:input:InvalidNumericExpressionso callers can present a friendly retry loop.
Example
value = input("Enter a scalar: ")Using input with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how input changes the result.
Run a small input example, explain the result, then change one input and compare the output.
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.
What does input do in MATLAB?⌄
input(prompt) displays the prompt string and waits for the user to type a response. The response is evaluated as a MATLAB expression. Use input(prompt, 's') to return the raw text as a string without evaluation.
How do I get string input from the user in MATLAB?⌄
Use str = input('Enter text: ', 's'). The 's' flag tells MATLAB to return the typed text as a character vector instead of evaluating it as an expression.
What is the difference between input and keyboard in MATLAB?⌄
input pauses for a single value from the user and returns it, while keyboard drops into an interactive debugging session. RunMat supports input for scripted user interaction.
Can I validate user input in MATLAB?⌄
Use a while loop: keep calling input until the value passes your validation check. For example, while x < 0; x = input('Enter positive number: '); end.
Does input work in RunMat's browser sandbox?⌄
Yes. input prompts the user within the RunMat sandbox. In non-interactive contexts, it returns the default empty value.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · mkdir · movefile · path · pwd · rmdir · rmpath · run · savepath · setenv · tempdir · tempname · uigetfile · uiputfile
Tabular
csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · readmatrix · spreadsheetImportOptions · writecell · writematrix · xlsread
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite
Import
Json
Archive
Http
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how input is executed, line by line, in Rust.
- View the source for input in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.