Changing decimal numbers in variable editor in MATLAB

Let's assume we have this code:

a = pi;
b = pi+2;
c = pi*2.2
out = [a b c] 

      

returns:

out =

    3.1416    5.1416    6.9115

      

I want this output (no rounding to two decimal places):

out =

    3.14    5.14    6.91

      

I don't want to print these values. I want to see them with two decimal places here: enter image description here

+3


source to share


2 answers


Use format bank

2 decimal places to display. Read more about the function format

in the documentation .

If you want to change the way variables are displayed in the variable editor, take a look at this documentation page .



enter image description here

enter image description here

+5


source


If you just want to use the first 2 decimal places, you can add this line to your previous code:

out(:) = str2num(sprintf('%6.2f',out(:))); % 2 = number of decimals

      



It's not a pretty solution, but it truncates your values ​​to the 2nd decimal place and keeps the following decimal places erased. You will still have some trailing zeros (until you fill in the chosen format for your variable editor as described above ).

+1


source







All Articles