MATLAB: The values are lost after the decimal point if I use the double type in the script

earth scienceera-interimMATLABprecipitationreanalysis datatime hours

Hi everyone! I really need some help. I downloaded the Era-interim reanalysis data (total precipitation over Eastern Europe, step 0.125) from 1979 to August 2019 and now I'm trying to write a script with my primitive programming skills. I need to pull out cases of heavy rainfall (>= 50 mm/12 hours) in a table or array representing the year, month, date, coordinates (latitude, longitude) and time. The problem is that after running the script in the displayed result, the decimal values are lost in the coordinate cells, although I use a double type. I split this array into pr50 and th50, thinking that this will solve the problem, but no. Maybe something wrong with my script?
tp=ncread('data.nc','tp'); % 'tp'-153x73x188 824
tp=tp*1000;
for i=1:153
for j=1:73
k(i,j,:)=movsum(tp(i,j,:),4);
end;
end;
lon1=load('coords.txt'); % my coordinates of the Ukraine borders
lon1=lon1';
lonn=(lon1-22)/0.125+1;
lon2=int32(lonn);
n=0;
for k1=3:118883
for j1=6:69
for i1=lon2(1,j1-5):1:lon2(2,j1-5)
if d1.k(i1,j1,k1)>=50
n=n+1;
doublelo1=(i1-1)*0.125+22;
la1=53-(j1-1)*0.125;
pr50(n,:)=double([d1.k(i1,j1,k1),lo1,la1]);
th50(n,:)=int32([d1.timehours(k1,1:4)]);
end;
end;
for i1=lon2(3,j1-5):1:lon2(4,j1-5)
if d1.k(i1,j1,k1)>=50
n=n+1;
lo1=(i1-1)*0.125+22;
la1=53-(j1-1)*0.125;
pr50(n,:)=double([d1.k(i1,j1,k1),lo1,la1]);
th50(n,:)=int32([d1.timehours(k1,1:4)]);
end;
end;
end;
end;

Best Answer

Let's look at the relevant pieces of your code.
lon2=int32(lonn);
n=0;
for k1=3:118883
for j1=6:69
for i1=lon2(1,j1-5):1:lon2(2,j1-5)
if d1.k(i1,j1,k1)>=50
n=n+1;
doublelo1=(i1-1)*0.125+22;
la1=53-(j1-1)*0.125;
pr50(n,:)=double([d1.k(i1,j1,k1),lo1,la1]);
th50(n,:)=int32([d1.timehours(k1,1:4)]);
end;
end;
By the first line, lon2 is of type int32. You can see this in your Workspace window.
Because i1 takes on values from lon2, it too is an int32 array. Since it's a scalar the Workspace window shows the actual value without the type, but you can see this by displaying i1 or asking for its class.
doublelo1 is created by performing arithmetic involving an int32 array and double scalars. By the "Arithmetic Operations on Integer Classes" section on this documentation page "MATLAB can perform integer arithmetic on the following types of data: ... Integers or integer arrays and scalar double-precision floating-point numbers. This yields a result that has the same data type as the integer operands:" So doublelo1 too is an int32 array (and is inaccurately named.)
la1 is a double array since it's created by doing arithmetic involving only double precision values.
I don't see anywhere that you've defined the variable lo1, so I assume that's a mistake when you copied over the code and it's supposed to be doublelo1. That means the line where you store data into pr50 you're concatenating together three arrays: whatever d1.k is, an int32, and a double. What happens if you combine integer and noninteger data? This page says it takes the data type of the left-most integer.
You immediately pass that data into the double function to convert it into a double array, but the conversion to an int32 array (or a different type of integer, if d1.k is some other integer type) has already occurred.
So let me take a step back. Why are you converting to int32? If you want to convert your values to the nearest integer value you don't need to convert it to an integer type. Just call round on your double data instead.