MATLAB: I am new to Matlab and I am trying to use some codes which generate some errors. Please help me to correct the following errors: Error: File: sampleStats.m Line: 31 Column: 64 Expression or statement is incorrect–possibly unbalanced (, {, or [.

{error: file: samplestats.m line: 31 column: 64 expression or statement is incorrect--possibly unbalanced (or

function [se ,k,mu ,sk ,mi ,ma ]= sampleStats ( filename , startDate , endDate )
% Get daily close and date data
[~,~ , rawData ]= xlsread ( filename ,'','','basic ');
date_cell = rawData (2: length ( rawData ) ,1);
close_cell = rawData (2: length ( rawData ) ,2);
date = zeros (1, length ( date_cell ));
close = zeros (1, length ( close_cell ));
% Convert the data to string format
for i =1: length ( date )
date (i)= datenum ( cell2mat ( date_cell (i)));
close_i = cell2mat ( close_cell (i));
if iscellstr ( close_i )
close (i)= str2num ( close_i );
else
close (i)= close_i ;
end
end
date = fliplr ( date );
close = fliplr ( close );
% Convert the date format to a correct one
date = x2mdate ( date );
% First and last days of the forecast will be on index
t1= find (date >= datenum ( startDate ) ,1);
t2= find (date >= datenum ( endDate ) ,1);
% Throw errors if data is missing
if isempty (t1) || isempty (t2)
msg = cell2mat ( strcat ('Lacking sufficient data in ' ,{' '},
filename ;', data avaliable for period ’ ,{' '}, datestr ( date (1)) ,{'
- '}, datestr ( date ( length ( date )))));
error (msg )
end
% Calculate the returns and the logaritmized returns
r= zeros ( length ( date ) ,1);
for i=t1 :1: t2
r(i)= close (i)/ close (i -1);
end
logr = log(r);
% Cut away non relevant days ( those that lack returns and those
after the
% period )
date = date (t1:t2);
close = close (t1:t2);
logr = logr (t1:t2);
se= std( logr );
k= kurtosis ( logr );
mu= mean ( logr );
sk= skewness ( logr );
mi= min( logr );
ma= max( logr );
end

Best Answer

The lines
msg = cell2mat ( strcat ('Lacking sufficient data in ' ,{' '},
filename ;', data avaliable for period ’ ,{' '}, datestr ( date (1)) ,{'
- '}, datestr ( date ( length ( date )))));
need to be written all as one line, or you need to use the ... continuation marker.
msg = cell2mat ( strcat ('Lacking sufficient data in ' ,{' '}, filename ;', data avaliable for period ’ ,{' '}, datestr ( date (1)) ,{' - '}, datestr ( date ( length ( date )))));
Also, you cannot use semi-colon at that point, as you are not within [] .
Furthermore, if you look very carefully at the character after "for period" it is ’ which is char(8217) which is Right Single Quotation, where you need ' instead.
Putting these together, you get
msg = cell2mat ( strcat ('Lacking sufficient data in ' ,{' '}, filename ,', data available for period ' ,{' '}, datestr ( date (1)) ,{' - '}, datestr ( date ( length ( date )))));