Measuring distance in 2D plot

85 次查看(过去 30 天)
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);

采纳的回答

Scott MacKenzie
Scott MacKenzie 2021-6-22
编辑:Scott MacKenzie 2021-6-22
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 个评论
Scott MacKenzie
Scott MacKenzie 2021-6-22
编辑:Scott MacKenzie 2021-6-22
Hmm, OK, I get it. You want to interact with the graph, not draw lines in the graph. Not sure if this sufficient, but If you use the brush tool, you can interactively get the x-coordinates between lines, and therein get the x length:
Addison Collins
Addison Collins 2021-6-22
Ah, I actually didn't think of the brush tool! It will get the job done.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Vector Fields 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by