# How to make algorithm using raster file?

4 views (last 30 days)
Son Thanh Tran on 9 Jul 2020
Commented: Son Thanh Tran on 9 Jul 2020
Hello every one, I have a problem regarding raster file in Matlab. My task is import the raster file about temperature to Matlab, then create a formula to calculate a parameter using data from that temperature raster file. Could you explain me how to import raster file and write the formular? The formula is in below, all of data are constant, I just need to put GDDakt (the value of temperature raster file) in. Thank you a lot. KSSV on 9 Jul 2020
How/ where is your raster file?
Image Analyst on 9 Jul 2020
What do you mean by raster? To me, I only think of it in regards to an image, like the image is produced by a raster scan where the electron beam goes from left to right and top to bottom. Do you simply mean that GDD is a matrix? If so, the "raster" adjective is not even needed - it just confuses things.
Son Thanh Tran on 9 Jul 2020
Hello, sorry for making you confuse. My supervisor has a data source about temperature, water... in the .tif format (they are all raster). What he wants me to do is trying to import those raster data in Matlab to calculate the suitable index for the tree. So in the formular, E is the suitable index for the tree. GDD max and GDD min are set by myself as threshold, the only thing to do is puting the raster data in the formula to calculate the suitable index E for each pixcel, and from that I can have suitable index to for tree species. The code is a bit similar to this SAS code. Im trying to find the way to create the same code in matlab KSSV on 9 Jul 2020
If your raster data matrix is A, to get your formula use:
GDDmax = max(A(:)) ; % this gives max of A
GDDmin = min(A(:)) ; % this gives min of A
E = 4*(GDDmax-A).*(A-GDDmin)/(GDDmax-GDDmin)^2 ;

Son Thanh Tran on 9 Jul 2020
Hello, thank you for your help. If i want to set many thresholds for GDD max and GDD min with different species, how can i write it in the form of If...then?. For example, I have 2 species: Fi and La. My raster data matrix is A. I will set GDDmax=2510, GDD min=150
E = 4*(2510-A).*(A-150)/(2510-150)^2
I want to set: If A>2510, then FiGDD = 0 (FiGDD is the E of species Fi)
If A<1200 then LaGDD = 4*(2290-A).*(A-110)/(2290-110)^2
If A>=1200 and GDD<=1500 then LaGDD = 1
Could you help me to write this formula in Matlab? Sorry, im totally new with matlab so im trying to ask. Thank you
Son Thanh Tran on 9 Jul 2020
When i write the code, all of value equal to 0. Could you check it for me? Image Analyst on 9 Jul 2020
Since you say your GDD data is in a TIFF file, you can try to use imread() to read in the tif image. If that doesn't work, there is a TIFF class with more functionality.

#### 1 Comment

Son Thanh Tran on 9 Jul 2020
Yes, it has already worked. Do you know how to write the formula? Im trying to write as advise of KSSV
So my formula is E = 4*(2510-A).*(A-150)/(2510-150)^2
A is data of GDD. However, all of results are 0. Does matlab automatically change the small number like 0.005 to 0? and if it is like that, how to change the number back to 0.005?