MATLAB: Storing value in a variable

MATLABmatlab functionvariable

According to my knowledge, the in x we can store values from -127 to 127. then why on executing this code, x=250 and x=3*10^12 is getting stored inx?
int8 x
intmax("int8")
x=35
x=250
x=30000000000000000000

Best Answer

It looks like you are trying to declare a variable type. MATLAB does not have variable type declarations.
This is what your MATLAB code is actually doing:
int8 x % convert the character 'x' to int8.
intmax("int8") % call INTMAX for "int8".
x=35 % define a double with value 35, give it the variable name x
x=250 % define a double with value 250, give it the variable name x
x=30000000000000000000 % define a double with value 3e20, give it the variable name x
None of these commands have anything to do with each other. None of them have any effect on each other.
The first two lines assign their outputs to the default ans. The behavior of the first line is explained here:
Here are the two common ways in MATLAB of converting a value to a particular class, either specify the class:
>> x = int8(250)
x = 127
or using indexing to assign to an existing array of that class:
>> x = zeros(1,1,'int8');
>> x(1) = 250
x = 127