Hi
I have a code where i run a couple of nc-files. I have make a for loop so I can run it over all the time steps and for all the longitudes and latitudes.
But I got an error called "Invalid use of opretor in the line called "zx(:,1:ny) = (:,ny:-1:1)z(n);"
I dont know why and what can I do?
I want to find blocking in the northern hemisphere where I will call if GHGS > 0 or GHGN < -10 there is a blocking. Did I have make a correct if loop?
for i = 1:Nfiles
lon{i} = ncread(ncfiles(i).name, 'longitude'); nx = length(lon{i});
lat{i} = ncread(ncfiles(i).name, 'latitude'); ny = length(lat{i});
time{i} = ncread(ncfiles(i).name, 'time'); nt = length(time{i});
z{i} = ncread(ncfiles(i).name, 'z'); nz = length(z{i});
end
%Midler geopotentialet til et månedsmiddel
zmean = zeros([nx ny]);
blocks = zeros([nx]);
for n = 1:nt
z = ncread(ncfiles(i).name,'z',[1 1 n],[nx ny 1]);
zx(:,1:ny) = (:,ny:-1:1)z(n);
zmean = zmean + zx;
%pcolor(lon,lat,z');
%shading interp
%drawnow
GHGS = (zx(:,[151+[-1 0 1]])-zx(:,[131+[-1 0 1]]))/20;
GHGN = (zx(:,[171+[-1 0 1]])-zx(:,[151+[-1 0 1]]))/20;
for i=1:ny
blocks(i)=blocks(i)+1;
if GHGS > 0;
disp('The point is blocked')
elseif GHGN < -10;
disp('The point is blocked')
end
end
end

 採用された回答

Jan
Jan 2018 年 12 月 20 日
編集済み: Jan 2018 年 12 月 20 日

0 投票

I have no idea, what (:,ny:-1:1)z(n) should do. Something essential is missing here. Maybe you mean - a bold guess:
zx(:,1:ny) = z(:,ny:-1:1) * z(n);
% ^ ^
In you other question this line is:
zx(:,1:ny) = z(:,ny:-1:1);
So something went wrong during editing.
By the way, follow the hints given in the editor:
% [151+[-1 0 1]] easier and more efficient:
151+[-1 0 1] % or
[150, 151, 152]
and
blocks = zeros(nx); % Without brackets
Note that this is equivalent to:
blocks = zeros(nx, nx); % not blocks = zeros(nx, 1)!
Because you use a single index running from 1 to ny:
for i=1:ny
blocks(i) = ...
I guess, that you want this instead:
blocks = zeros(1, ny); % or zeros(ny,1)
The line
if GHGS > 0;
does not need a trailing semicolon and might not do, what you expect: if requires a scalar condition, therefore Matlab evaluates implicitly:
if all(GHGS > 0) && ~isempty(GHGS)
I assume you mean GHGS(i).

5 件のコメント

Jonas Damsbo
Jonas Damsbo 2018 年 12 月 27 日
編集済み: Jonas Damsbo 2018 年 12 月 27 日
Hi Jan
I got a question to my code line:
z = ncread(ncfiles(i).name,'z',[1 1 1],[nx ny 1]);
Here I use the syntax for ncread:
vardata = ncread(source,varname,start,count)
I got the error "Index exceeds array bounds. Where nx and ny is longitudes and latitudes:
nx = 360
ny = 181
Index exceeds array bounds.
Error in Blocking (line 39)
z = ncread(ncfiles(i).name,'z',[1 1 1],[nx ny 1]);
Jan
Jan 2018 年 12 月 28 日
Use the debugger to find out, which index is exceeded:
dbstop if error
Then start the code again. When Matlab stops at the error, check the variables and indices:
which ncread % is it a variable or the function?
size(ncfiles)
i
Walter Roberson
Walter Roberson 2018 年 12 月 28 日
Try
z = ncread(ncfiles(i).name,'z',[1 1 1],[ny nx 1]);
NCF uses rows and columns reversed from MATLAB notation.
Jonas Damsbo
Jonas Damsbo 2018 年 12 月 28 日
編集済み: Jonas Damsbo 2018 年 12 月 28 日
Okay, I have found something thats maybe could say something?
If I writing return just here:
for i = 1:Nfiles
lon{i} = ncread(ncfiles(i).name, 'longitude');
lat{i} = ncread(ncfiles(i).name, 'latitude');
time{i} = ncread(ncfiles(i).name, 'time');
z{i} = ncread(ncfiles(i).name, 'z');
end
nx = length(lon{i});
ny = length(lat{i});
nt = length(vertcat(time{:}));
zmean = zeros([nx ny]);
blocks = zeros(nx);
return
%Writing the data over all time steps
for n = 1:nt
z = ncread(ncfiles(i).name,'z',[1 1 n],[nx ny 1]);
zx(:,1:ny) = z(:,ny:-1:1);
zmean = zmean + zx;
end
I can see my z variable is a cell array like this:
So it's longitudes x latitudes x time.
Are there a way to combine these cells?
Walter Roberson
Walter Roberson 2018 年 12 月 28 日
cell2mat() can combine them. But you need to ask which dimension you want to combine them over. You would probably need to
cell2mat(reshape(z, 1, 1, []))
to combine them over the third dimension (you cannot combine them over the 4th dimension because the third dimensions do not match.)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

質問済み:

2018 年 12 月 20 日

コメント済み:

2018 年 12 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by