- Formula that you are trying to code in the given example.
- One or two sample inputs for the function.
how do add a line between two points on an existing graph and calculate the area in between
1 view (last 30 days)
Show older comments
I have an output of a cross section of a river and would like to calculate the area that has been eroded. I need to add a line between the two points in the figure and calculate the area that is highlighted. I know how to do this by hand, but was wondering if there was a way to do it in MatLab. This is the code I've been using and have been changing the (z, dx, row1, column1, row2, column2, npts) to different numbers based on different locations of the river.
function [dprofile, zprofile] = profileacrossgrid(z, dx, row1, column1, row2, column2, npts)
rowprofile = linspace(row1, row2, npts); % row indices to interpolate
columnprofile = linspace(column1, column2, npts);
dprofile = dx * sqrt((rowprofile-row1).^2 + (columnprofile-column1).^2);
zprofile = interp2(z, columnprofile, rowprofile);
figure
plot(dprofile, zprofile)
xlabel 'Distance (m)'
ylabel 'Elevation (m)'
1 Comment
Suvansh Arora
on 4 Nov 2022
In order to understand this better, could you please provide the following information:
Answers (1)
Marcel
on 7 Nov 2022
I dont know if this helps but i used to make a picture, draw a line on it and calculate the distance of the line i had drawn and converted the pixel to mm (milimeter) later on.
% Make a picture from the camera
frame = getsnapshot(vid);
% Show the image taken and draw a line
dsp = imshow(frame);
line = drawline();
ep = line.Position;
% Get the position of the start and end of the line
x1 = ep(1,1);
y1 = ep(1,2);
x2 = ep(2,1);
y2 = ep(2,2);
% Optional. Draw the line onto the picture with a label of the start and
% end location
frame = insertText(frame, [x1 y1], "1", "FontSize", 8);
frame = insertText(frame, [x2 y2], "2", "FontSize", 8);
frame = insertShape(frame, 'Line', ...
[x1 y1 x2 y2], 'Color', "blue", "LineWidth", 4);
% Calculate the distance in pixel
val = sqrt((y2-y1)^2+(x2-x1)^2);
% Show the image
imshow(frame);
% Pixel to mm
inMM = px / double(16.0343);
i used this code to make a picture of a ruler and draw a line between the milimeter lines so i know how many pixels would be one milimeter. In my case one milimeter was ~16 pixels. If you have something in your image you know the size/length of, you can do the same. but im jsut sharing my experience to get you an idea
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!