필터 지우기
필터 지우기

Creating and Calling functions

조회 수: 1 (최근 30일)
Macarena Santillan
Macarena Santillan 2021년 5월 26일
댓글: Macarena Santillan 2021년 6월 7일
Hello,
I want to create a function that will take a data set, extract desired columns/rows , take their average and compute the difference.
I am new to this and I am not sure why I am getting errors, this is what I have so far. Does anyone know where I went wrong? I thought the function maybe should be the last line of my code, but that did not work either.
Also, as I have 20+ data sets I want to do this analysis on, can I do it on the same script or do I need a new different file?
Thank you!
%Create a functions that computes the averages of plateaus and difference between them.
function
data=readmatrix(x);
%extract desired rows and columns
x1=data(13911:111401,1);
y1=data(13911:111401,2);
x2=data(216399:1000015,1);
y2=data(216399:1000015,2);
Mx1= mean(x1,'all');
My1 = mean(y1,'all');
Mx2= mean(x2,'all');
My2 = mean(y2,'all');
mvd= (My2- My1);
end

답변 (1개)

Walter Roberson
Walter Roberson 2021년 5월 26일
function
data=readmatrix(x);
That needs to be on a single line.
Mx1= mean(x1,'all');
Using 'all' is not wrong, but it is unnecessary, as you are working with vectors.
Note that you compute the x means but you do not do anything with them.
Note that you do not return mvd, only data.
  댓글 수: 17
Walter Roberson
Walter Roberson 2021년 6월 7일
dinfo = dir('tek*.csv');
filenames = {dinfo.name};
num_files = length(filenames);
results = zeros(num_files,5);
for K = 1 : length(filenames)
results(K,:) = computingmvdifference(filenames{K});
end
%Create a functions that computes the averages of plateaus and difference between them.
function results = computingmvdifference(x)
data = readmatrix(x);
%extract desired rows and columns
x1=data(13911:111401,1);
y1=data(13911:111401,2);
x2=data(216399:1000015,1);
y2=data(216399:1000015,2);
Mx1 = mean(x1,'all');
My1 = mean(y1,'all');
Mx2= mean(x2,'all');
My2 = mean(y2,'all');
mvd = (My2- My1);
results = [mvd, Mx1, My1, Mx2, My2];
end
After the code finishes, then
results(:,1) -- will be all of the mvd values
results(:,2) -- will be all of the Mx1 values
results(:,3) -- will be all of the My1 values
results(:,4) -- will be all of the Mx2 values
results(:,5) -- will be all of the My2 values
Macarena Santillan
Macarena Santillan 2021년 6월 7일
Thank you so much! this works great!!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by