Writing a new function

조회 수: 10 (최근 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by