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