Error in making gif file from a netcdf file
1 view (last 30 days)
Show older comments
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
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))
Answers (0)
See Also
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!