MATLAB Answers

Measuring distance in 2D plot

7 views (last 30 days)
Hello everyone,
I am attempting to measure the horizontal length between two almost parallel streamlines. I would like to simply draw a line on the plot and have it display the length in the X-direction (not the length of the line itself). Is this an easy thing to do? I could not find it in the plot tools. I have attached the code and the mat file used in this.
Edit: In the photo below, I show what I have been attempting to do.
% PIV streamlines code
clc;clear;close all
file = dir('*.txt');
filename = file.name;
data = readmatrix(filename);
% Min and max of the data x and y
kx = 0; % mm - Shifting factor
ky = 0;
spanX = [min(data(:,1))+kx max(data(:,1 ))+kx];
spanY = [min(data(:,2))+ky max(data(:,2 ))+ky];
% 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 a grid
Uq = griddata(data(:,1),data(:,2),data(:,3),Xq,Yq);
Vq = griddata(data(:,1),data(:,2),data(:,4),Xq,Yq);
xtip = -4.5;%-1.5; % Bottom left vertice of left probe wall
ytip = -7.703; % Bottom left vertice of left probe wall
height = 12.7; % mm
width = 1; % mm
gap = 3; % mm - Distance between probe walls (left end of the walls)
%% Plot vectors, probe walls, and streamlines
figure()
% quiver(Xq,Yq,Uq,Vq, 1)%, 'LineWidth',2) % Plot vectors
% quiver(Xq,Yq,Uq,Vq)
title(['Averaged Velocities and Streamlines'])
% Plot probe walls
rectangle('Position',[xtip,ytip,width,height],'FaceColor','k')
rectangle('Position',[xtip+gap,ytip,width,height],'FaceColor','k')
% Plot streamlines
xlabel('x (mm)')
ylabel('y (mm)')
startx = -10:0.4:6 ;
starty = -20*ones(size(startx ));
hgridinterp = streamline(Xq,Yq,Uq,Vq,startx,starty );
set(hgridinterp,'color','red','linewidth',3);

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 22 Jun 2021
Edited: Scott MacKenzie on 22 Jun 2021
Just adding this to the end of your code achieves what you are after (I think). The x-length of the line easy enough to calculate, or am I missing something?
% test coordinates for line, as per photo in question
xx = [-3.5 -1.75];
yy = [-17 -19];
% draw line
line(xx, yy, 'linewidth', 5);
% compute x-length
xlength = abs(min(xx)-max(xx));
% display x-length
text(-3, -16, sprintf('x-length=%.2f', xlength), 'backgroundcolor', 'w');
  3 Comments
Addison Collins
Addison Collins on 22 Jun 2021
Ah, I actually didn't think of the brush tool! It will get the job done.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!