Mean values of dynamic intervals

2 views (last 30 days)
NB
NB on 29 Aug 2019
Edited: Adam on 29 Aug 2019
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 Comment
Adam
Adam on 29 Aug 2019
Edited: Adam on 29 Aug 2019
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.

Sign in to comment.

Answers (0)

Categories

Find more on Environment and Settings 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!