making a function work

I've written the following function which imports .txt files into matlab. The .txt files can either be recorded at hourly intervals or 4minute intervals where depending on the initial resolution the script will calculate the hourly or daily averages.
function [Daily, Hourly] = calc_avg(pathName)
TopFolder = pathName;
dirListing = dir(fullfile(TopFolder,'*.txt'));%Lists the folders in the directory specified
%by pathName.
for i = 1:length(dirListing);
fileToRead1{i} = (dirListing(i).name);
%lists all of the .txt files in the TopFolder
end
cell_all = arrayfun(@(i1)importdata(fullfile(pathName,dirListing(i1).name)),...
(1:length(dirListing))','un',0);
%apply function to each element of the array.
d = cat(2,cell_all{:});
%concatenate arrays along each column (i.e. 2)
%find the length of the dataset, which will provide information on the
%amount of averaging required.
if length(d) == 365,...
error('error: daily averages already calculated');
elseif length(d) == 8760;
daily = squeeze(mean(reshape(d,24,size(d,1)/24,[])));
else length(d) == 131400;
hourly = squeeze(mean(reshape(d,15,size(d,1)/15,[])));
daily = squeeze(mean(reshape(d,360,size(d,1)/360,[])));
end
%find which averages have been calculated:
A = exist('hourly','var');
%if A == 1 it means that hourly values had to be calculated therefore
%the data if of high resolution (minutes).
if A == 1;
hourly = mat2cell(hourly,size(hourly,1),cellfun('size',cell_all,2)).';
daily = mat2cell(daily,size(daily,1),cellfun('size',cell_all,2)).';
elseif A == 0;
daily = mat2cell(daily,size(daily,1),cellfun('size',cell_all,2)).';
end
%create cell in the same format as 'cell_all' where cellfun applies the
%same function to each cell in a cell array. 'size' is used to create
%the same format.
for i=1:length(dirListing);
[~,name{i}] = fileparts(fileToRead1{i});
%obtain the name of each of the .txt files (dirListing)
end
%Generate a structure for the averages calculated.
if A == 1;
for i=1:length(dirListing);
Daily.(genvarname(name{i})) = daily{i};
Hourly.(genvarname(name{i})) = hourly{i};
end
elseif A == 0;
for i=1:length(dirListing);
Daily.(genvarname(name{i})) = daily{i};
end
end
The script works fine if I simply run it as a script i.e. avoid using the function and just type the path Name into the second line.
What am I missing which would make this work as a function?

 Accepted Answer

Jan
Jan on 16 Feb 2012

1 vote

This should work well as a function also. How do you call it and do you get an error message?

6 Comments

Richard
Richard on 16 Feb 2012
The function is called calc_avg.m. So, the steps that I take are as follows:
cd('...')% directory where the function is stored.
calc_avg('ThePathName');
This will return nothing in the workspace. I dont really know what I should call the variable!
If I type
a = calc_avg('ThePathName')
Error using calc_avg
Too many output arguments.
Jan
Jan on 16 Feb 2012
The function cala_avg does not reply anything. If you want to export e.g. Daily and Hourly you have to add this in the first line:
function [Daily, Hourly] = calc_avg(pathNam)
Richard
Richard on 16 Feb 2012
I receive the following error when changing the first line
Error in calc_avg (line 15)
TopFolder = pathName;
Output argument "Hourly" (and maybe others) not assigned during call to "E:\University\\calc_avg.m>calc_avg".
Jan
Jan on 16 Feb 2012
Please, lestyn, read the error message and fix the code accordingly. The message is clear enough.
If A is 0, "Hourly" is not defined. But I cannot know, how you want to handle this case. Perhaps you want to create a default value as NaN?
Jan
Jan on 16 Feb 2012
Btw. if you have checked a logical variable by "if A == 1", an "else" is enough and you can omit the "elseif A == 0".
Richard
Richard on 16 Feb 2012
Great, thank you.

Sign in to comment.

More Answers (1)

Maybe the path is messed up, what is 'ThePathName'? I'd suggest to put calc_avg on the path, then cd to the directory where the files are located, and call the following
calc_avg(pwd)
to see if it fixes the issue.

3 Comments

Richard
Richard on 16 Feb 2012
If I save the function in the same file as the directory that the files are located then it produces a variable 'ans' which include the values from 'Daily' in the function above. But if I write:
[Daily, Hourly] = calc_avg(pwd);
It gives an error:
Error in calc_avg (line 15)
TopFolder = pathName;
Output argument "Hourly" (and maybe others) not assigned during call to "E:\University\\calc_avg.m>calc_avg".
NOTE: function above ammended.
As Jan mentioned in his comment, in this case Hourly is not defined. Therefore you need to put something default for Hourly if A is 0. Maybe add something like Hourly = [] or Hourly = nan;
Richard
Richard on 16 Feb 2012
I amended Hourly =[] and it worked fine, thank you.

Sign in to comment.

Tags

Asked:

on 16 Feb 2012

Edited:

on 15 Oct 2013

Community Treasure Hunt

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

Start Hunting!