subscript indices must either be real positive integers or logicals - how to change lat/lons from double to integers?

1 次查看(过去 30 天)
Hi,
I am executing a script for a global grid, using for i=1:144 for j=1:91 [z]=SPI ...etc end end
But I get the error message: subscript indices must either be real positive integers or logicals
The lat and lons were originally 'double' format in the source netcdf file... is there an easy way to change them to integers in matlab?

采纳的回答

Geoff
Geoff 2012-5-16
If they just need to be indices, you can use ceil, floor, fix or round. eg
x = fix(x)
If you want to convert them to ACTUAL integer types, you can use:
x = uint32(x);
[EDIT] As the question changes...
As Walter pointed out, you are overwriting Z every loop. Perhaps you wanted Z indexed by i and j... In which case:
Z(i,j) = SPI(precip30y_model1(i,j),12,12);
But since you said that Z is empty after iteration, is it possible that SPI() can return an empty set instead of a scalar? In that case:
Z = nan(144,91);
for i=1:size(Z,1)
for j=1:size(Z,2)
s = SPI(precip30y_model1(i,j),12,12);
if ~isempty(s)
Z(i,j) = s;
end
end
end
That uses NaN to represent "no data".
[EDIT 2012-05-23]
Okay, from your comments I think you want this:
Z = nan(144,91);
for i=1:size(Z,1)
for j=1:size(Z,2)
Z(i,j) = SPI(squeeze(precip30y_model1(i,j,:)),12,12);
end
end
  18 个评论
Walter Roberson
Walter Roberson 2012-5-24
nccreate() creates a scalar variable unless you specify dimension information.
To fix the other problem:
dimid(1) = netcdf.defDim(ncid,'time',348);
dimid(2) = netcdf.defDim(ncid,'lon',144);
dimid(3) = netcdf.defDim(ncid,'lat',91);
Madeleine P.
Madeleine P. 2012-5-26
Walter Roberson, your experiment seems to have worked! I'm now outputting netcdf files with spi. Thanks so much to you and Geoff for your suggestions and persistence in helping me! (even when I was about to give up). Cheers, M.P.

请先登录,再进行评论。

更多回答(1 个)

Madeleine P.
Madeleine P. 2012-5-18
Didn't come up with an answer for this - added in the time for loop, reran the script, takes 6 hours to run, and I still end up with an empty array, Z[] (!). No error messages.
?????

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by