Main Content

ans

Most recent answer

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.

example

Examples

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
result = 
8
ans
ans = 
4

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.

testFunc
ans = 
75

Version History

Introduced before R2006a