Using For Loop in a function over all files in a folder with Matlab

1 view (last 30 days)
Dear Matlab Users,
I want to use for loop in a function over all files in a folder.
I want to apply the genhurst function over all files in a folder.
H = genhurst(S)
where S is the time series with 1*6154 vector.
Here, I am explaining the details of the genhurst function as follows.
function [mH,sH]=genhurst(S,q,maxT)
if nargin < 2, q = 1; maxT = 19; end
if nargin < 3, maxT = 19; end
if size(S,1)==1 & size(S,2)>1
S = S';
elseif size(S,1)>1 & size(S,2)>1
fprintf('S must be 1xT \n')
if size(S,1) < (maxT*4 | 60)
warning('Data serie very short!')
lq = length(q);
H = [];
k = 0;
for Tmax=5:maxT
k = k+1;
x = 1:Tmax;
mcord = zeros(Tmax,lq);
for tt = 1:Tmax
dV = S((tt+1):tt:L) - S(((tt+1):tt:L)-tt);
VV = S(((tt+1):tt:(L+tt))-tt)';
N = length(dV)+1;
X = 1:N;
Y = VV;
mx = sum(X)/N;
SSxx = sum(X.^2) - N*mx^2;
my = sum(Y)/N;
SSxy = sum(X.*Y) - N*mx*my;
cc(1) = SSxy/SSxx;
cc(2) = my - cc(1)*mx;
ddVd = dV - cc(1);
VVVd = VV - cc(1).*(1:N) - cc(2);
%hold on
%hold on
%plot([1 N-1],mean(dV)*[1 1],'-b')
%plot([1 N-1],mean(ddVd)*[1 1],'-r')
for qq=1:lq
mx = mean(log10(x));
SSxx = sum(log10(x).^2) - Tmax*mx^2;
for qq=1:lq
my = mean(log10(mcord(:,qq)));
SSxy = sum(log10(x).*log10(mcord(:,qq))') - Tmax*mx*my;
H(k,qq) = SSxy/SSxx;
mH = mean(H)'./q(:);
if nargout == 2
sH = std(H)'./q(:);
elseif nargout == 1
sH = [];
In a folder, I have 20 Excel files having 2 columns in each file.
The first column represents the date whereas the 2nd column represents the time series.
Further, I want to calculate the monthly average of the estimated values for each time series.
Please help me to loop this function, and take the monthly average of the estimated values over all the files in a folder.
Thank you.
RP on 28 Oct 2019
S is the column vector, and q and maxT are defined in the function. I want to calculate genhurst for the multiple column vectors on a monthly basis.
Here, I arranged each vector in separate excel files, and attached folder path in the code. However, one can arrange all the column vectors in 1 excel file, and calculate the function by looping across the columns. I tried this way also but unable to that.
Please help in this regards.
Thank you.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!