How to derive/integrate plotted data?
3 views (last 30 days)
Show older comments
I have data points that I'm plotting from an excel sheet that I would like to create a derivate or integral plot of. I've been plotting them via duration because the data is time coded, but I think having the x-axis be time is preventing me from getting derivaties or integrals.
I've tried diff to create derivatives but all I get is a perfectly flat line at 0. it looks like dy is being stored as a sym?
I've tried cumtrapz to get an integral but all I get is the base graph.
How do i fix this and actually get the derivative plot of my graph? And would a similar process follow for create an integral?
%
clear
datasource = "xxxxxxxxxxx.xlsx"
%importing data
disp("Importing Flight Data...")
Sheet1 = readtable(datasource,'PreserveVariableNames',true);
disp("Done")
disp("Importing Division Data...")
Sheet2 = readtable(datasource, 'sheet', 2, 'ReadRowNames',false, 'VariableNamingRule','preserve');
disp("Done")
%}
format longG;
TimetoEnd=(height(Sheet1)-1)*0.032*1000;
numManuRows=height(Sheet2);
MaxVar=(width(Sheet1)-1);
%plot x axis with time
t=duration(12,00,00,000:032:TimetoEnd,"format","hh:mm:ss.SSS");
tdoub=milliseconds(t/86400000);
Prompt which variable
while true
prompt = "Which variable would you like to see? Type 0 to end. ";
i = input(prompt);
if or(i<=0,MaxVar<i)
disp ("-------------End-------------")
break
end
y = eval(sprintf('Sheet1.Variable%i',i));
clf
plot(t,y,'k'); %plot graph
hold on;
pause(3);
%what follows is me trying to plot the derivative
syms x;
dy=diff(y,tdoubint);
plot(t,dy,'b');
%naming graph
graphname="Variable %d Derivative";
str2=sprintf(graphname,i);
title (str2);
%
disp("Creating Divisions...")
for m=1:numRows
dividename=Sheet2.ID{m};
dividetime = hours(Sheet2.Time(m) * 24);
dividetime.Format = 'hh:mm:ss.SSS';
if isnan(Sheet2.Duration(m)) == 1;
xline(dividetime,'g',{dividename});
elseif mod(m,2) == 1
xline(dividetime,'r',{dividename});
else
xline(dividetime,'b',{dividename});
end
end
%}
disp("Done")
%end
2 Comments
Torsten
on 10 Jun 2025
We don't have your data - so we cannot run your code.
I suggest plotting the inputs to "diff" and "cumtrapz" just before you call the functions for differentiation and integration.
There is no need to define symbolic variables to use these functions.
Walter Roberson
on 10 Jun 2025
y = eval(sprintf('Sheet1.Variable%i',i));
Instead of using eval, you can use
y = Sheet1.("Variable" + i));
Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!