Combining plots of 3 different variables into a single plot

6 次查看(过去 30 天)
I am trying to make a contour plot from an output netcdf file from my oceanic simulation. I am using a netcdf file and want a single plot with variables u,v and w . The dimension of variables I am using are as follows:
u
Size: 99x50x15x273
Dimensions: xi_u,eta_u,s_rho,ocean_time
Datatype: single
Attributes:
long_name = 'u-momentum component'
units = 'meter second-1'
time = 'ocean_time'
grid = 'grid'
location = 'edge1'
coordinates = 'lon_u lat_u s_rho ocean_time'
v
Size: 99x50x15x273
Dimensions: xi_v,eta_v,s_rho,ocean_time
Datatype: single
Attributes:
long_name = 'v-momentum component'
units = 'meter second-1'
time = 'ocean_time'
grid = 'grid'
location = 'edge1'
coordinates = 'lon_v lat_v s_rho ocean_time'
w
Size: 99x50x15x273
Dimensions: xi_u,eta_w,s_rho,ocean_time
Datatype: single
Attributes:
long_name = 'w-momentum component'
units = 'meter second-1'
time = 'ocean_time'
grid = 'grid'
location = 'edge1'
coordinates = 'lon_w lat_w s_rho ocean_time'
I am unable to write a matlab script for it that plots the combined effect of all these variables that is total = sqrt( u*u + v*v +w*w) into a plot.
It would be of great help if anyone could help me with the script as I am a beginner in MATLAB and need to do this for my project purpose. I have attached my file
I am attaching the code I wrote and I know it's not even correct to any degree:
File = Lombok_roms_his.nc
Lat = ncread(File,lat);
Lon = ncread(File,lon);
U = ncread(File,u);
V = ncread(File,v);
W = ncread(File,w);
Total = sqrt(U*U + V*V + W*W);
pcolor(lon,lat,Total(:,:,:,1)')
[m,n,p] = size(Total) ;
for i = 1:p
pcolor(Lat,Lon,Total(:,:,:i)')
shading interp
colorbar
drawnow
end
  1 个评论
Shankhaneel Basak
Shankhaneel Basak 2021-5-27
When I run the above code written by me I get the following error and I am not sure how to remove this in order to achieve my target:
Array dimensions must match for binary array op.
Error in Untitled (line 7)
Total = sqrt((U.*U) + (V.*V) + (W.*W));

请先登录,再进行评论。

回答(1 个)

Shubham Khatri
Shubham Khatri 2021-6-7
Hello,
I was not able to reproduce the scenario from the file provided. But to my understanding, this error occurs when the matrix dimensions are not same. You are performing additions in your code. To do this you need to have the exact same dimensions for all the matrices. Please take a look at the code below. For better understanding, if you change the 4th dimension of matirx A from 7 to 6, you ll get the same error.
A = zeros(2,3,5,7);
A(1,2,1,2) = 6;
A(1,1,5,6) = 5;
A(2,1,1,:) = pi;
B = zeros(2,3,5,7);
B(1,2,1,1) = 2;
B(1,1,5,6) = 4;
B(1,1,1,:) = 5;
Total = sqrt((A.*A) + (B.*B));
Hope it helps
  1 个评论
Shankhaneel Basak
Thanks a lot for your answer but if it were a dimension error, then why does it happen in my case? u,v,w has the same size of 99x50x15x273 . The parameters are different but sizes are same. In that case addition or any such operation should be working, shouldn't it ?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Weather and Atmospheric Science 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by