ocean current velocity data m_quiver plot on high resolution coast line refreshdata not working
Show older comments
I have u & v ocean current forecast velocity data and I need to plot every three hours in a loop using 8 days worth of data, then use this plot to create an animated gif. If I loop the map projection its very slow but works. So I was hoping to only refresh the plot data but refreshdata is either not working or more likely than not, I am not using it correctly. Could someone please review my code and see what I am doing wrong or please point me in the right direction. I updated my code with the suggestion below and was still unable to get it to work
This animated gif below is what I am aiming to accomplish with some more efficient code. Right now its super slow because it loops the code that creates the map and axis with the high resolution coastline (the coastline slows this down significantly) and m_quiver plot each iteration.

get_vel_data
global start_stamp
global tstamp
t = datenum(DateTime, 'yyyy-mm-ddTHH:MM:SSZ');
t = datetime(t,'ConvertFrom','datenum','Format', 'yyyy-MM-dd HH:mm:ss');
% start = '2015-11-14 00:00:00';
start = datetime('2015-12-01 00:00:00', 'Format', 'yyyy-MM-dd HH:mm:ss');
start_stamp = datestr(start);
ds = dataset(t,Lat,Lon,u_vel,v_vel);
%%Animated Plot
figure(99)
filename = 'testnew72.gif';
% set map and grid
axes('Position',[0.1,0.1,0.8,0.8]);
m_proj('albers equal-area','lat',[50 65],'lon',[185 215]);
m_gshhs_h('patch',[.6 .6 .6]);
m_grid('linest','none','linewidth',2,'tickdir','out','xaxisloc','top','yaxisloc','right');
tiHan = title(['U and V Ocean Current Velocity Forecast Data Downloaded on ',start_stamp], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
% axis x, y value of title position
tiPos = get (tiHan, 'position');
xyrange = axis;
% move title up
set(tiHan, 'position', tiPos + [0 0.05 * (xyrange(4) - xyrange(3)) 0]);
% so Title doesn't move on zoom.
set(tiHan, 'units', 'inches');
xlabel(['Forecast Data for ',tstamp], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
hold on;
lat = 0;
lon = 0;
u = 0;
v = 0;
uv_plot = m_quiver(lon,lat,u,v);
% looping through the 64 different time frames. 24hrs/(3hr fcasts)*8days=64
for n = 1:64
if n==1;
i=0;
else
i=i+3;
end
% update timestamp for axis label
tstamp = start + hours(i);
tstamp = datestr(tstamp);
% uv_plot.UDataSource = 'u';
% uv_plot.VDataSource = 'v';
% uv_plot.XDataSource = 'lat';
% uv_plot.YDataSource = 'lon';
% xlabel([], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
% update temp dataset
t0 = ds(ds.t == start + hours(i),{'t','Lat','Lon','u_vel','v_vel'});
lat = t0.Lat;
lon = t0.Lon;
u = t0.u_vel;
v = t0.v_vel;
uv_plot.UData = 'u';
uv_plot.VData = 'v';
uv_plot.XData = 'lat';
uv_plot.YData = 'lon';
drawnow()
xlabel(['Forecast Data for ',tstamp], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
% clf;
frame = getframe(99);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
Answers (2)
Walter Roberson
on 21 Dec 2015
Instead of refreshdata() in the loop, use
t0 = ds(ds.t == start + hours(i),{'t','Lat','Lon','u_vel','v_vel'});
lat = t0.Lat;
lon = t0.Lon;
u = t0.u_vel;
v = t0.v_vel;
uv_plot.UData = U;
uv_plot.VData = v;
uv_plot.XData = lat;
uv_plot.YData = lon;
and the drawnow() will do the update.
refreshdata() is slower than setting the parameters to their desired values.
You also do not need the
xlabel([], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
as you change the xlabel again a few lines later.
11 Comments
Jon Rendulic
on 21 Dec 2015
Edited: Jon Rendulic
on 21 Dec 2015
Walter Roberson
on 21 Dec 2015
The post does not appear to have the changed code?
Jon Rendulic
on 21 Dec 2015
Edited: Jon Rendulic
on 22 Dec 2015
Jon Rendulic
on 2 Jan 2016
Walter Roberson
on 4 Jan 2016
I have not only refreshed my browser cache, and not only exited and reentered my browser: I have even upgraded the browser version. And your code still uses
uv_plot.UDataSource = 'u';
uv_plot.VDataSource = 'v';
which is code that is only needed if you are using refreshdata(), and your code does not have
uv_plot.UData = U;
uv_plot.VData = v;
uv_plot.XData = lat;
uv_plot.YData = lon;
like I showed needs to be done.
Do net set the DataSource variables. Do not use refreshdata(). Set the *Data variables each trip through the loop and drawnow() after you have updated all of them.
Jon Rendulic
on 7 Jan 2016
Edited: Jon Rendulic
on 7 Jan 2016
Jon Rendulic
on 7 Jan 2016
Walter Roberson
on 9 Jan 2016
uv_plot.UData = u;
uv_plot.VData = v;
uv_plot.XData = lat;
uv_plot.YData = lon;
Walter Roberson
on 9 Jan 2016
You have coded
t0 = ds(ds.t == start + hours(i),{'t', 'Lat', 'Lon', 'u_vel', 'v_vel'});
The problem with that is that you are comparing a precise time down to the second, against what appears to be the beginning of an hour.
It appears to me that the safest approach is to use two comparisons:
mask = ds.t >= start + hours(i) & ds.t < start + hours(i+1);
t0 = ds(mask,{'t', 'Lat', 'Lon', 'u_vel', 'v_vel'});
It is tempting to use isbetween() instead of two comparisons, but isbetween is closed interval and so would include the upper bound, which you do not want.
Jon Rendulic
on 10 Jan 2016
Jon Rendulic
on 10 Jan 2016
Kyon
on 12 Feb 2017
0 votes
i want to do the map in 3d
1 Comment
Walter Roberson
on 12 Feb 2017
What map do you want to do in 3D?
And do you have the Mapping Toolbox?
Categories
Find more on Discrete Data Plots 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!