0
votes

I have a matrix (type:double) of size 106 x 103. The matrix represents European gridded temperature data for one timestep (a day).

For every day, I want to calculate the degree days (thermal time) for a species, based on the temperature recorded for each 'cell' (i,j element) in the matrix using a formula that I have coded in Matlab based on a sinewave approach.

So, ultimately, what I want to do is being able to apply a calculation to my matrix, that will provide individual output for each grid cell (i,j element) dependent on the temperature data that is recorded there. I could do this with a loop, but I have to accumulate these degree days for multiple years, so I would prefer to find a way of applying the calculation to each element in a daily matrix simultaneously (and then looping through the days (matrices)). From what I have read, you can use a cellfun if your matrix is a cell array (mine is not). Then I was also looking at the bsxfun option, but it seems like the functions are just standard functions..like mean, max etc. So now, I'm looking at using arrayfun in conjunction with a function I create from my algorithm to calculate degree days. I have been trying to write a test function but Matlab keeps throwing up the same error: I type:

function output=degreedays(x)

and Matlab throws back:

Error: Function definitions are not permitted in this context.

Can someone tell me what I'm doing wrong? Why is it not accepting the declaration of the function name?

1
function... is function definition. It cannot happen everywhere and generally only happens at the top of the file containing said function. Once you have the function, you use it just like outStuff = degreedays(yourData);Zizy Archer

1 Answers

1
votes

MATLAB does not allow you to define named functions like this at the command line. You need to place your function definition in a file. MATLAB then can call that function by the name of the file - so in your case, put your function definition in a file called degreedays.m.

See the doc for more: https://uk.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html .