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!
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:
For a better understanding, please refer to the following documentation: https://in.mathworks.com/help/matlab/ref/matlab.io.datastore.tabulartextdatastore.html#mw_7e1955a2-061d-495c-9122-07c4ad79de55
Further, for finding the area under curve for values upto a certain 'x', kindly attach your text file for reproducing the issue.
Star Strider on 11 Jul 2022
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
patch([x(Lv); flipud(x(Lv))], [ones(size(y(Lv)))*min(y(Lv)); flipud(y(Lv))], 'b', 'FaceAlpha',0.5)
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.