storing value in a variable

51 views (last 30 days)
ashwin TP
ashwin TP on 7 Sep 2020
Edited: Stephen23 on 7 Sep 2020
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

Accepted Answer

Stephen23
Stephen23 on 7 Sep 2020
Edited: Stephen23 on 7 Sep 2020
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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!