ocean current velocity data m_quiver plot on high resolution coast line refreshdata not working

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)

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

that line of code below:
xlabel([], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
This changes the xlabel so that it clear out the previous timestamp because each loop writes the new time and if I do not clear it it will write it on top of itself and looks like a blurry mess of text.
The drawnow() did not work by the way.
I update the post to show all of my code, to include the change you suggested.
It does on my end. You may want to refresh your browser or clear your temp internet files or cache. I made the change and it does not plot. Thoughts?
Have you checked back on this? Any updates or thoughts on this?
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.
I commented out those 5 lines you mentioned and added
uv_plot.UData = U;
uv_plot.VData = v;
uv_plot.XData = lat;
uv_plot.YData = lon;
U is not a variable so I fixed that to a lowercase u but then it still does not draw the vectors and is blank each loop. So I changed the code to:
uv_plot.UData = 'u';
uv_plot.VData = 'v';
uv_plot.XData = 'lat';
uv_plot.YData = 'lon';
And I get the following error:
While setting the 'UData' property of 'Quiver': Value must be an array of numeric type with 3 or fewer dimensions
Error in ocean_current_uv_velocity_forecast_test (line 84) uv_plot.UData = 'u';
I also updated the main post to show my current code. Sorry about that before I now understand what you were pointing out.
uv_plot.UData = u;
uv_plot.VData = v;
uv_plot.XData = lat;
uv_plot.YData = lon;
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.
I made the change and it still does not draw the vectors.
Keep in mind that this code originally worked hence the animated gif. It runs fine in a loop but is extremely slow.
The reason I made this post and decided to try using drawnow or refreshdata was to make my code more efficient. I was looking for a smarter way to solve this problem.
This is the code that I have that works but is extremely slow.
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 = 'testnew99.gif';
% set map and grid
% 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);
% update temp dataset
t0 = ds(ds.t == start + hours(i),{'t','Lat','Lon','u_vel','v_vel'});
lat0 = t0.Lat; lon0 = t0.Lon;
u0 = t0.u_vel; v0 = t0.v_vel;
clf;
v_plot(lat0,lon0,u0,v0)
% pause(.05);
drawnow
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
The code for v_plot is:
function v_plot(lat0, lon0, u0, v0)
global start_stamp
global tstamp
axes('Position',[0.1,0.1,0.8,0.8]);
m_proj('albers equal-area','lat',[55 60],'lon',[200 210]);
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);
tiPos = get (tiHan, 'position'); % axis x, y value of title position
xyrange = axis;
set(tiHan, 'position', tiPos + [0 0.05 * (xyrange(4) - xyrange(3)) 0]); % move title up
set(tiHan, 'units', 'inches'); % so Title doesn't move on zoom.
xlabel(['Forecast Data for ',tstamp], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
hold on;
m_quiver(lon0,lat0,u0,v0);

Sign in to comment.

Categories

Asked:

on 21 Dec 2015

Commented:

on 12 Feb 2017

Community Treasure Hunt

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

Start Hunting!