Mean values of dynamic intervals

조회 수: 2 (최근 30일)
NB
NB 2019년 8월 29일
편집: Adam 2019년 8월 29일
Hello everyone,
I want to calculate the mean values of several intervals, which shall automatically defined and calculate before. In addition, I want to do this for multiple excel files at the same time and the script also shall write the mean values of these intervals in an xls-file.
I want the script to find intervals in row B(2) of my xls-files and to calculate the mean value of the values from row C(3). Intervals will begin with <-0.05 and end with >0.05.
I wrote the following script, but it doesnt work. Maybe someone can help me?
P.S.: I am a beginner, just tried to collect information and write the script cause i need it pretty soon ;)
function EXAMPLE()
clear all;
clc;
tic
%------------------------
folder='C:\MATLAB_NB\Rate';
filetype='*xls';
f=fullfile(folder,filetype)
d=dir(f)
for k=1:numel(d)
data{k}=xlsread(fullfile(folder,d(k).name));
end
%------------------------
TIME = 2; % Time
HR = 3; % Rate
%------------------------
interval = data(:,3)
value = data(:,2)
%------------------------
q = ~(interval < -.05 | interval > .05)
dq = diff([false q false])
ix1 = find(dq==1)
ix2 = find(dq == -1) - 1
%------------------------
section = @(k) mean(value(ix1(k):ix2(k)))
R = arrayfun(section, 1:numel(ix1)) % mean of each section
%------------------------
xlswrite('myexcelfile.xls');
toc
  댓글 수: 1
Adam
Adam 2019년 8월 29일
편집: Adam 2019년 8월 29일
What aspect doesn't work? What happens?
As an aside, don't put
clear all
clc
in a function. A function has its own workspace, there is nothing in it at the start of the function if no input arguments are passed in so 'clear all' does nothing (and even worse if there are input arguments it would instantly delete them).
clc also has no business being in a function whose purpose is to run some other code. A function should have a clear purpose and stick to that, without having side effects that someone calling the function doesn't expect it to. Obviously in this case you may be the only person ever using the function, it's just a comment related to general good design.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by