0

So I am currently working with different datasets. Some are monthly, some daily, but I want quarterly. This is why I wrote the following function:

function y = average2(data, frequency)

% Monthly/Daily data to quarterly data by taking average  
% INPUT     data     Nx2      monthly/daily data
% OUTPUT    y        Mx2      quarterly data
% USAGE     average2(data)

if frequency == 'monthly';
   K = 1:3:(length(data)-3);                      
   quarterly = (data(K, 2)+data(K+1, 2)+data(K+2, 2))/3; 
   timevector = data(K, 1);
   y = [timevector quarterly];

elseif frequency == 'daily';
   y = data*data;                                   %just as an example, not correct calculation

else frequency ~= 'daily' || 'monthly';
   error('Requested frequency not available');

end

(the calculation of daily is not the problem). So my Problem is the following: If I use the monthly option, everything works fine. But everytime I use something different than 'monthly' as frequency in my function, I get the error message:

 Matrix dimensions must agree.

 Error in average2 (line 8)
 if frequency == 'monthly';

Therefore activating the elseif clause and processing the input I get in frequency doesn't work. Does anyone know where I have a mistake? Thanks in advance

1 Answer 1

1

To compare strings, use the strcmp (case sensitive) or the strcmpi (case insensitive) functions.

if(strcmp(frequency,'monthly'))
   K = 1:3:(length(data)-3);                      
   quarterly = (data(K, 2)+data(K+1, 2)+data(K+2, 2))/3; 
   timevector = data(K, 1);
   y = [timevector quarterly];

elseif(strcmp(frequency,'monthly'))
   y = data*data;                                   %just as an example, not correct calculation

else % frequency ~= 'daily' || 'monthly' % don't have to do this comparison and is not correctly coded
   error('Requested frequency not available');
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.