How to add arrow to streamcolor plot?
4 ビュー (過去 30 日間)
古いコメントを表示
Hello everyone,
I work with Matalb2018b and I have a vector field that I'd like to represent using streamline representation such like what is done by the following code (I also plot a contourf on another quantity on the same graph):
figure; clf; hold on;
[C1,hh1] = contourf(X(indy,indx,siz_z/2)/d,Y(indy,indx,siz_z/2)/d,wzz(indy,indx)*d/Vp,levels,'linecolor','none'); %contourf
colormap(map); colorbar; caxis(caxisval);
[vertices,arrowvertices] = streamslice(X(indy(1:p:end),indx(1:p:end),siz_z/2)/d,Y(indy(1:p:end),indx(1:p:end),siz_z/2)/d...
,U(indy(1:p:end),indx(1:p:end),siz_z/2,ind(i))/Vp,V(indy(1:p:end),indx(1:p:end),siz_z/2,ind(i))/Vp,density);
hh = streamslice(X(indy(1:p:end),indx(1:p:end),siz_z/2)/d,Y(indy(1:p:end),indx(1:p:end),siz_z/2)/d...
,U(indy(1:p:end),indx(1:p:end),siz_z/2,ind(i))/Vp,V(indy(1:p:end),indx(1:p:end),siz_z/2,ind(i))/Vp,density);
set(hh,'color','k','linewidth',1.5)

The thing I want to change is the color of the streamline depending on the magnitude of the vector field at each point. I tried the streamcolor function that I found here on the matlab exchange, and I have quite what I want except this function is not made to plot arrows such as streamline.
Here it what it look likes:
figure; clf; hold on;
[C1,hh1] = contourf(X(indy,indx,siz_z/2)/d,Y(indy,indx,siz_z/2)/d,wzz(indy,indx)*d/Vp,levels,'linecolor','none');
colormap(map); colorbar; caxis(caxisval);
startxx = []; startyy = [];
for j = 1:length(vertices)
startxx(j) = vertices{j}(1,1); % I use the values of vertices from the preceding streamline plot to specify the start position of the streamline
startyy(j) = vertices{j}(1,2);
end
hh = Streamcolor_v4(X(indy(1:p:end),indx(1:p:end),siz_z/2)/d,Y(indy(1:p:end),indx(1:p:end),siz_z/2)/d...
,U(indy(1:p:end),indx(1:p:end),siz_z/2,ind(i))/Vp,V(indy(1:p:end),indx(1:p:end),siz_z/2,ind(i))/Vp,startxx, startyy,vnorm,cmap)

I use a colormap going from white to black so that streamlines corresponding to low values of the vector field are not seen.
Here is the code for the function streamcolor_v4.m which is a modified version of what is found on the exchange to specify the colormap.
function hout=Streamcolor_v4(varargin)
%STREAMCOLOR: COLOR Streamlines from 2D or 3D vector data.
% H = STREAMCOLOR(X,Y,Z,U,V,W,STARTX,STARTY,STARTZ,VMAG) creates streamlines
% from 3D vector data U,V,W. The arrays X,Y,Z define the coordinates for
% U,V,W and must be monotonic and 3D plaid (as if produced by MESHGRID).
% STARTX, STARTY, and STARTZ define the starting positions of the stream
% lines. A vector of line handles is returned.
%
% H = STREAMCOLOR(U,V,W,STARTX,STARTY,STARTZ,VMAG) assumes
% [X Y Z] = meshgrid(1:N, 1:M, 1:P) where [M,N,P]=SIZE(U).
%
% H = STREAMCOLOR(X,Y,U,V,STARTX,STARTY,VMAG) creates streamlines from 2D
% vector data U,V. The arrays X,Y define the coordinates for U,V and
% must be monotonic and 2D plaid (as if produced by MESHGRID). STARTX
% and STARTY define the starting positions of the streamlines. A vector
% of line handles is returned.
%
% H = STREAMCOLOR(U,V,STARTX,STARTY,VMAG) assumes
% [X Y] = meshgrid(1:N, 1:M) where [M,N]=SIZE(U).
%
% 3D Example:
% load wind
% [sx,sy,sz] = meshgrid(80,10:5:60,1:2:15);
% X=x; Y=y; Z=z; U=u; V=v; W=1*w;
% Vmag=sqrt(U.^2+V.^2+W.^2);
% streamcolor(X,Y,Z,U,V,W,sx,sy,sz,Vmag)
% 2D Example:
% load wind
% [sx,sy] = meshgrid(80,10:60);
% X=x(:,:,5); Y=y(:,:,5); U=u(:,:,5); V=v(:,:,5);
% Vmag=sqrt(U.^2+V.^2);
% streamcolor(X,Y,U,V,sx,sy,Vmag);
% grid on; axis image
% See also STREAM3, STREAM2, CONEPLOT, ISOSURFACE, SMOOTH3, SUBVOLUME,
% REDUCEVOLUME.
% Bertrand Dano 05-05-2009
% Copyright 1984-2009 The MathWorks, Inc.
lw=2; % width of the streamlines
[verts x y z u v w sx sy sz Vmag cmap] = parseargs(nargin,varargin);
Vmax=max(Vmag(:));
streamoptions = [0.1 250];
if isempty(verts)
if isempty(w) % 2D
if isempty(x)
verts = stream2(u,v,sx,sy,streamoptions);
else
verts = stream2(x,y,u,v,sx,sy,streamoptions);
end
else % 3D
if isempty(x)
verts = stream3(u,v,w,sx,sy,sz);
else
verts = stream3(x,y,z,u,v,w,sx,sy,sz);
end
end
end
h = [];
for k = 1:length(verts)
vv = verts{k};
if ~isempty(vv)
if size(vv,2)==3
X=vv(:,1); Y=vv(:,2); Z=vv(:,3);
Vcol=uint8(floor(interp3(x,y,z,Vmag,X,Y,Z)/Vmax*size(cmap,1)));
Vcol(Vcol==0)=1;
for j=1:size(cmap,1)
pos = find(Vcol==j);
if(~isempty(pos) && pos(1)==1), pos(1)=[]; end
if ~isempty(pos)
tempx = [X(pos-1) X(pos) NaN(size(pos))]';
tempy = [Y(pos-1) Y(pos) NaN(size(pos))]';
tempz = [Z(pos-1) Z(pos) NaN(size(pos))]';
h = [h ; line(tempx(1:end-1),tempy(1:end-1),tempz(1:end-1),'color',cmap(j,:),'linewidth',lw)];
end
end
else
X=vv(:,1); Y=vv(:,2);
Vcol=uint8(floor(interp2(x,y,Vmag,X,Y)/Vmax*size(cmap,1)));
Vcol(Vcol==0)=1;
for j=1:size(cmap,1)
pos = find(Vcol==j);
if(~isempty(pos) && pos(1)==1), pos(1)=[]; end
if ~isempty(pos)
tempx = [X(pos-1) X(pos) NaN(size(pos))]';
tempy = [Y(pos-1) Y(pos) NaN(size(pos))]';
h = [h ; line(tempx(1:end-1),tempy(1:end-1),'color',cmap(j,:),'linewidth',lw)];
end
end
end
end
end
if nargout>0
hout=h;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [verts, x, y, z, u, v, w, sx, sy, sz, Vmag,cmap] = parseargs(nin, vargin)
verts = [];
x = [];
y = [];
z = [];
u = [];
v = [];
w = [];
sx = [];
sy = [];
sz = [];
Vmag = [];
cmap = [];
if nin==1 % streamline(xyz) or streamline(xy)
verts = vargin{1};
if ~iscell(verts)
error('Stream vertices must be passed in as a cell array')
end
elseif nin==4 | nin==5 % streamline(u,v,sx,sy)
u = vargin{1};
v = vargin{2};
sx = vargin{3};
sy = vargin{4};
if nin==5, Vmag = vargin{5}; end
elseif nin==6 | nin==7 % streamline(u,v,w,sx,sy,sz) or streamline(x,y,u,v,sx,sy)
u = vargin{1};
v = vargin{2};
if ndims(u)==3
w = vargin{3};
sx = vargin{4};
sy = vargin{5};
sz = vargin{6};
else
x = u;
y = v;
u = vargin{3};
v = vargin{4};
sx = vargin{5};
sy = vargin{6};
end
if nin==7, Vmag = vargin{7}; end
% if nin == 8, values = vargin{8}; end
elseif nin == 8
x = vargin{1};
y = vargin{2};
u = vargin{3};
v = vargin{4};
sx = vargin{5};
sy = vargin{6};
Vmag = vargin{7};
cmap = vargin{8};
elseif nin==9 | nin==10 % streamline(x,y,z,u,v,w,sx,sy,sz)
x = vargin{1};
y = vargin{2};
z = vargin{3};
u = vargin{4};
v = vargin{5};
w = vargin{6};
sx = vargin{7};
sy = vargin{8};
sz = vargin{9};
if nin==10, Vmag = vargin{10}; end
if nin==11, cmap = vargin{11}; end
else
error('Wrong number of input arguments.');
end
sx = sx(:);
sy = sy(:);
sz = sz(:);
My question if the following: do you have any idea how to add arrows on the streamcolor plot such as what is done by streamline?
Any help will be appreciated :)
Thants in advance and if you need any clarification, do not fear to ask :)
0 件のコメント
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Animation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!