Error in making gif file from a netcdf file

1 view (last 30 days)
Hello people !
I am fairly new to MATLAB and am trying to make an animation in gif format from an output netcdf file from my oceanic simulation. I face the error which I am pretty sure is dumb as the message is crystal clear in the command window. It says that :
">> animation_velocity
Error using surf (line 71)
Data dimensions must agree.
Error in animation_velocity (line 13)
surf(lon,lat,u_vel(:,:,:,i)) "
My net cdf file has the variable i want to plot has following details:
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'
ocean_time
Size: 273x1
Dimensions: ocean_time
Datatype: double
Attributes:
long_name = 'time since initialization'
units = 'seconds since 2000-01-01 00:00:00'
latitude: Size: 100x50, Dimensions: xi_rho,eta_rho ; longitude: Size: 100x50 , Dimensions: xi_rho,eta_rho
I am using the following matlab script taking notes from the post https://in.mathworks.com/matlabcentral/answers/354954-how-to-make-animation-from-netcdf-file#answer_280209 which is something like this :
file = 'Lombok_roms_his.nc';
latitude = ncread(file,'lat_rho');
longitude = ncread(file,'lon_rho');
mask = ncread(file,'mask_rho');
time = ncread(file,'ocean_time');
u_vel = ncread(file,'u');
lat = double(latitude);
lon = double(longitude);
msk = double(mask);
filename = 'u_vel.gif';
for i = 1:length(time)
surf(lon,lat,u_vel(:,:,:,i))
shading interp
view(2)
drawnow
% Capture the plot as an image
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if i == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
  2 Comments
Chad Greene
Chad Greene on 19 Apr 2021
Is your variable u the same as your variable u_vel? If so, its dimensions are 99x50x15x273, whereas lat and lon are 100x50. The first step will be getting some coordinates that correspond to u.
Second step: If you pick only one time-step of the variable u by taking u(:,:,:,i) then you'll have a 99x50x15 matrix (technically 99x50x15x1, but we can ignore the singleton dimension in this case). To plot a surface you'll need to pick a single (depth level?) or whatever is specified by the third dimension. So to plot the first depth level, that would look like
surf(lon,lat,u(:,:,1,i))
Shankhaneel Basak
Shankhaneel Basak on 25 May 2021
So by saying "getting some coordinates that correspond to u." , you mean to say I need to convert my lat and lon variables to 99 x 50 ? If I were to do so let's say by creating a seperate variable for lat and lon so that I can store just 99x50 dimension of the original lat and lon into those two new variable, how am I supposed to do that? Shall for loop do the job:
for i = 1: 99
for j = 1:50
lon_new = lon[i][j]
lat_new = lat[i][j]
end
end
A code like this ? Then could I use these new variables for the plot ? Any help is appreciated by me.
Thanks a lot again

Sign in to comment.

Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!