MATLAB Answers

Plotting streamlines from a dataset (x,y,u,v)

4 views (last 30 days)
I am attempting to plot streamlines from some location and velocity data provided in a .txt. I have x, y, u, v where x & y are positions and u & v are their corresponding velocity components. I would like to plot streamlines and vectors of this data. Everything is in vector form with 345600 elements. I have manually determined I have 640 unique x values and 540 unique y values. Essentially there is matrix that can be made from this in the form of (640 x 540). I attempted utilizing meshgrid(x,y) and meshgrid(u,v). But this led to issues due to the resulting size when you mesgrid two vectors of 345600.
I have a poor understanding of how exactly meshgrid works. Given the nature of the table I am provided, is it necessary to meshgrid (x, y) and meshgrid (u, v)? Or should I reshape the x, y, u, & v vectors in order to get matrices that can be used in streamlines(x,y,u,v,startx,starty)?
Note: If this code does require a meshgrid for x ,y, u, & v, then it will almost certainly be necessary to reduces the resolution of the data to make calculations. Perhaps only using every 5th value. Also, I think the resolution will cause the arrows and streamlines too be too close together (?).
clear all; close; clc;
ntot = 345600;
n1 = 100000; n2 = 101000;
filename = 'run27_avg';
data = readmatrix(strcat('.\average_velocities\',filename,'.txt'));
x = data(:,1);
% [fc,fia,fic]=unique(x)
% length(unique(x,'rows'))
x = x(n1:n2, 1);
y = data(:,2);
y = y(n1:n2, 1);
[x,y] = meshgrid(x,y);
u = data(:,3);
u = u(n1:n2, 1);
v = data(:,4);
v = v(n1:n2, 1);
[u,v] = meshgrid(u,v);
startx = 0.1:0.1:1;
starty = ones(size(startx));

Accepted Answer

Joseph Cheng
Joseph Cheng on 2 Jun 2021
Edited: Joseph Cheng on 2 Jun 2021
So, meshgrid generates a grid of x and grid of y values so index wise you get the xy pair-combo. usually very useful when you're building a grid of points. here is a bit of snippet of code that goes through for you the reshaping of data and meshgrid
close all
data = dlmread('run27_avg.txt',' ',1,0);
%just get the min and max of the data x and y
spanX = [min(data(:,1)) max(data(:,1))];
spanY = [min(data(:,2)) max(data(:,2))];
%generate equally spaced x and y arrays
Xq = linspace(spanX(1),spanX(2),640);
Yq = linspace(spanY(1),spanY(2),540);
%create the combination of xq and yq
[Xq, Yq]=meshgrid(Xq,Yq);
%interp the data if the x and y data in the file is not in an equally spaced grid
Uq = griddata(data(:,1),data(:,2),data(:,3),Xq,Yq);
Vq = griddata(data(:,1),data(:,2),data(:,4),Xq,Yq);
startx = 0.1:0.1:1;
starty = ones(size(startx));
hgridinterp = streamline(Xq,Yq,Uq,Vq,startx,starty);
%if in a grid you can reshape the data (looking at the file x is listed
%first from 1:640 for 540 times... so after reshaping to 640x540(rows x col)
%transpose it so x runs along the horizontal
reshapeX = reshape(data(:,1),640,540)';
reshapeY = reshape(data(:,2),640,540)';
reshapeU = reshape(data(:,3),640,540)';
reshapeV = reshape(data(:,4),640,540)';
for you it does look like that data is in a grid but not equally spaced. at first i thought streamline would care but i guess not. the X values (data(:,1)) jumps between 0.057 and 0.0571 delta values (difference between points).

More Answers (0)

Community Treasure Hunt

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

Start Hunting!