File Exchange

## Area under a curve calculation

version 1.0.0.0 (2.54 KB) by
Area can be calculated from pixels representation of the area Specially for irregular curves

Updated 01 Nov 2011

from pixels representation of the area Specially for irregular curves we can find the area accurately

### Cite As

Ahmed Owaid (2020). Area under a curve calculation (https://www.mathworks.com/matlabcentral/fileexchange/33570-area-under-a-curve-calculation), MATLAB Central File Exchange. Retrieved .

Abdallah Abu Aisha

Hi Ahmed, I really like your solution. However, I am working on something very similar but for huge number of trips, so the this solution doesn't work for my case. Just wanted to ask: did you, by any chance, find a more "practical" way to find the sum of all areas between two irregular curves, or line and irregular curve ( regardless positive or negative areas)? I have been struggling finding a function/code to solve this issue but all of them return only the "net" area. Best Regards, Abdallah

Stijn

Hi Ahmed!

Thanks for sharing your code. I've modified the values for my own use and it works perfectly well.

Small note: maybe consider replacing:

saveas(h,'xy.png');

with:

xy = print('-RGBImage');

This works as long as the function isn't called by a GUI.

Ahmed Owaid

Thanks ,I want to show that pixels representation can be used in area caculation when simple method fail for that specially for hysterese curves.

Sven

Oh, I see. polyarea() doesn't return what you expect for self-intersecting x-y coordinates.

Keep in mind that you don't need to save to a file then load from a file to follow your algorithm - you can grab pixels directly from the screen:

function Area = AreaUnderCurve(x,y)
minmaxXY = [min(x) max(x) min(y) max(y)];
myFig = figure('Color','w'); axis off
patch(x,y,'k','EdgeColor','none')
axis(minmaxXY)
figIm = getframe(gca);
close(myFig)
ratioOn = nnz(figIm.cdata==0) / numel(figIm.cdata);
squareArea = diff(minmaxXY(1:2)) * diff(minmaxXY(3:4));
Area = squareArea * ratioOn;

Sven

Is there a reason why polyarea(x,y) won't work? It seems a much simpler solution than plotting x and y, then saving the plot, then loading the saved image, then counting pixels.

##### MATLAB Release Compatibility
Created with R14SP2
Compatible with any release
##### Platform Compatibility
Windows macOS Linux