ans - Most recent answer - MATLAB (original) (raw)
Main Content
Syntax
Description
ans
is the variable created when an output is returned without a specified output argument. MATLABĀ® creates the ans
variable and stores the output there. Changing or using the value of ans
in a script or function is not recommended, as the value can change frequently.
ans
is specific to the current workspace. The base workspace and each function workspace can have its own instance of ans
. For more information, see Base and Function Workspaces.
Examples
Result of a Simple Calculation
Perform a simple calculation in the Command Window without assigning the result to a variable. MATLAB stores the result in the ans
variable.
Perform a simple calculation in the Command Window and assign the result to the variable result
.
Display the values of result
and then ans
. MATLAB displays the value of result
without returning an output. Therefore, the value of ans
remains unchanged.
Call a Function That Returns Output
Suppose you have a function testFunc
, which returns an output, without specifying an output variable.
function a = testFunc a = 75; end
Call testFunc
. MATLAB stores the returned result in ans
.
Version History
Introduced before R2006a