ans

Most recent answer

Syntax

ans

Description

example

When you run MATLAB® code that returns an output without specifying an 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

collapse 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

Introduced before R2006a