i is a scalar
state(i,3) = arrayfun(A, B);
3 is a scalar, and i is a scalar, so the left hand side represents a scalar. That can only work with the right hand side if the arrayfun() is outputing a scalar -- which would be an unusual use of arrayfun() but legal.
We cannot rule out the possibility yet that the arrayfun() is returning a scalar cell array, so there might be non-scalar data stored inside state(i,3)
max() cannot be used on a cell array, so in order for the code to work here, state(i,3) must be numeric, in which case it must also be scalar, and max() of it will be the same as the value state(i,3)
i_station = datasample(state(i,3), 1);
There is only one value in the scalar state(i,3) so datasample is going to return that one value.
j_station = datasample(state(i,3), 1);
Still only one value in the scalar, so sampling is going to return the one value.
If you change the code so that you have a vector of values, say state(i,3,:) then
station_values = datasample(state(i,3,:), 2, 'replace', false);
i_station = station_values(1);
j_station = station_values(2);