Writing a new function

1 次查看(过去 30 天)
Slane Haltine
Slane Haltine 2020-12-14
评论: Star Strider 2020-12-15
I have data for three trials which include: time, acceleration and angular velocity. I have found the peaks of each (acceleration), throughout the trial, by using MATLAB's 'findpeaks' function. They occur about half way through the trial.
I want to write a function that will calculate the elapsed time from the start of the trial to the peak acceleration.
I have included the data for the 3 trials that were used.
Here is my code for detecting the peaks.
'load'
for c=1:length(files)
Dataset(c).T = readtable(files(c).name); %loads all trials into dataset folder
end
%%Data processing: find the peaks
minThreshold = 17.5; %omits smaller peaks
for c=1:length(files)
findpeaks((Dataset(c).T.Acceleration_m_s_2_),'MinPeakHeight', minThreshold);
[peaks(c),loc(c)] = findpeaks((Dataset(c).T.Acceleration_m_s_2_),'MinPeakHeight', minThreshold);
hold on
end
xlabel('(Downhill Trial)')
ylabel('(m/s^(2))')
grid on
title('Acceleration peaks')
The code above works well to find the peaks and generate a plot including all three trials. However, next I am trying to create a function that is able to detect the elapsed time, from start to peak. I want to be able to apply the function to each indiviual trial so I can add more trials later.
I am very new to creating my own functions so I hope some help can be provided.

回答(1 个)

Star Strider
Star Strider 2020-12-14
I just used the first file.
Try this:
c = 1;
Dataset(c).T = readtable('T1 Centripetal acceleration 2020-12-07 10-23-09.xls', 'VariableNamingRule','preserve');
Q1 = Dataset(c).T(1:5,:);
VarNames = Dataset(c).T.Properties.VariableNames;
%%Data processing: find the peaks
minThreshold = 17.5; %omits smaller peaks
[peaks(c),loc(c)] = findpeaks(Dataset(c).T{:,3},'MinPeakHeight', minThreshold);
figure
plot(Dataset(c).T{:,1}, Dataset(c).T{:,3})
text(Dataset(c).T{loc(c),1}, Dataset(c).T{loc(c),3}, sprintf('\\uparrow\nElapsed Time = %.3f s', Dataset(c).T{loc(c),1}-Dataset(c).T{1,1}), 'VerticalAlignment','top', 'HorizontalAlignment','center')
xlabel('(Downhill Trial)')
ylabel('(m/s^(2))')
grid
title('Acceleration peaks')
Expand it to use all the files, and experiment with it to get the result you want.
  2 个评论
Slane Haltine
Slane Haltine 2020-12-15
编辑:Slane Haltine 2020-12-15
That does detect the elapsed time but I don't want to add it to the plot. Am I able to write a new function that is able to detect it and provide it as an output?
In the following format:
function [outputArg1,outputArg2] = untitled(inputArg1,inputArg2)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
outputArg1 = inputArg1;
outputArg2 = inputArg2;
end
Star Strider
Star Strider 2020-12-15
It is not necessary to add it to the plot. I did in order to provide an example of how I would do it.
Just use:
Elapsed_Time = Dataset(c).T{loc(c),1}-Dataset(c).T{1,1});
With respect to creating a function to do that, the function would have to assume a particular form of the input. Then just subtract the initial value from the peak value to calculate the elapsed time. It would liekly be possible to do that with an anonymous function. See the documentation section on Anonymous Functions for details.

请先登录,再进行评论。

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by