Main Content


Most recent answer



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.


expand all

Perform a simple calculation in the Command Window without assigning the result to a variable. MATLAB stores the result in the ans variable.

2 + 2
ans = 4

Perform a simple calculation in the Command Window and assign the result to the variable result.

result = 4 + 4
result = 8

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.

result = 8
ans = 4

Suppose you have a function testFunc, which returns an output, without specifying an output variable.

function a = testFunc 
a = 75;

Call testFunc. MATLAB stores the returned result in ans.

ans = 75
Introduced before R2006a