Area Under the Curve value from txt file that has two columns

2 views (last 30 days)
Hello! I was a txt file from a graph that has two columns (x and y value). How I get the values from that txt file into MATLAB and find the area under the curve for values upto a certain x = value? For example, I want to find the area under the curve for values under the x =400? Thank you for your help!

Answers (2)

Chandrika on 11 Jul 2022
As per my understanding, for finding area under the curve using values entailed over two columns in a .txt file, you want to import the data into MATLAB.
You can do this using a datastore to get the data from the.txt file to MATLAB.
Assuming, 'X' and 'Y' are the name of the two columns in the txt file and are of type integer, try using the following code:
ds = tabularTextDatastore("file.txt","TreatAsMissing","NA",...
ds.SelectedVariableNames = ["X","Y"];
ds.SelectedFormats = ["%d","%d"];
You can preview the data stored in the form a table in the datastore using:
Further, for finding the area under curve for values upto a certain 'x', kindly attach your text file for reproducing the issue.

Star Strider
Star Strider on 11 Jul 2022
Use either the readtable or readmatrix function to import it, then trapz to do the integration.
The integration would go something like this —
x = linspace(0, 600, 250).'; % Create 'x'
y = 10*(1-exp(-0.001*x)); % Create 'y'
Lv = x <= 400; % Logical Vector
AUC = trapz(x(Lv), y(Lv)); % Integration
plot(x, y)
hold on
patch([x(Lv); flipud(x(Lv))], [ones(size(y(Lv)))*min(y(Lv)); flipud(y(Lv))], 'b', 'FaceAlpha',0.5)
hold off
text(250, max(y(x<=200))/2, sprintf('AUC = %.3f', AUC), 'Horiz','center')
The plot simply illustrates the idea, and is not necessary for the area calculation.





Community Treasure Hunt

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

Start Hunting!

Translated by