Is the max. length of a string vector 1993?
Show older comments
Hi you all! The following progam should converte a .txt file containing rows of the following form into an array with numeric values and simultaniously ignore rows that don't contain the 'C' in column 5:
ODAX 19930104 9:30:56 1500.00 C 0 199301 N 47.00 20 20 .
I'm only interested in the columns 2,4,5,7 and 9.
My Programm works perfectly fine if the file I'm testing my program with only has 20 rows. As soon as I use the original file with 388056 rows I get the error:
??? Index exceeds matrix dimensions.
Displaying 'n' yields 1993, instead of the true value 388056.
If I let the first if-loop run until 1992 (weirdly not 1993) instead of 'n' it works again.
Can anybody help me? Is my memory capacity restricted somehow (I use matlab R2011a on a mac with mountainlion)
Thanks a lot in adavance!
Regards, Jens
clear all
close all
clc
%main programm
%import file
odax = fopen('odaxtest.txt');
B = textscan(odax, '%s%s%s%f%s%u%s%s%f%u%u%s');
%only take call options into account
n = length(B{5});
j=1;
for i=1:n
if strcmp(B{5}(i),'C') == 1
D{1}(j) = B{2}(i);
D{2}(j) = B{7}(i);
D{3}(j) = B{4}(i);
D{4}(j) = B{9}(i);
j=j+1;
end
end
%converte dates into serial date number
D{1} = datenum(datevec(D{1},'yyyymmdd'));
D{2} = datenum(datevec(D{2},'yyyymm'));
%set Final Settlement Day to the third friday of the expiration month
k = length(D{2})
for i=1:k
if D{2}(i)==datenum('1993-01')
D{2}(i)=datenum('1993-01-15');
elseif D{2}(i)==datenum('1993-02')
D{2}(i)=datenum('1993-02-19');
elseif D{2}(i)==datenum('1993-03')
D{2}(i)=datenum('1993-03-19');
elseif D{2}(i)==datenum('1993-04')
D{2}(i)=datenum('1993-04-16');
elseif D{2}(i)==datenum('1993-05')
D{2}(i)=datenum('1993-05-21');
elseif D{2}(i)==datenum('1993-06')
D{2}(i)=datenum('1993-06-18');
elseif D{2}(i)==datenum('1993-07')
D{2}(i)=datenum('1993-07-16');
elseif D{2}(i)==datenum('1993-08')
D{2}(i)=datenum('1993-08-20');
elseif D{2}(i)==datenum('1993-09')
D{2}(i)=datenum('1993-09-17');
elseif D{2}(i)==datenum('1993-10')
D{2}(i)=datenum('1993-10-15');
elseif D{2}(i)==datenum('1993-11')
D{2}(i)=datenum('1993-11-19');
else
D{2}(i)=datenum('1993-12-17');
end
end
% Data = [Maturity Stirke LTPrice]
Data = [D{2}-D{1} D{3}' D{4}'];
fclose(odax);
3 Comments
Jan
on 20 Mar 2013
Please care for posting a complete copy of the error message instead of mentioning parts of it only. It is important to know, which line causes the problems.
The brute clearing header "clear all, close all, clc" is usually a waste of time: Why removing all loaded functions from the memory, when reloading them from disk and parsing them is one of the most time consuming jobs of Matlab?!
Jens
on 20 Mar 2013
Jan
on 20 Mar 2013
Then please do not believe everything which you have been taught in the university. You find this brute clearing header on top of so many examples, that the students think, it must be useful. But as usual there is no correlation between the ratio of people, who follow a certain opinion, and the quality of the opinion itself. To test this, ask a lot of people, if the Spaceshuttle has been able to fly to the moon. Or ask Matlab users, if FOR loops are processed slowly in Matlab. Or count the number of Matlab beginners, who think that EVAL and EVALIN helps to solve problems.
So please ask your Matlab teacher, if he could be so kind to join a discussion about this header in this forum. It would be really helpful, if I could catch the teachers, instead of posting the warning hundreds of times to the students and pupils.
All error message I have seen before contain a copy of the failing line and the line number also, as well as the calling stack. Do you run this code from the command line? If so, store it in a function. At first you get more useful error messages, at 2nd you can use the debugger by setting breakpoints, at 3rd the code is calculated faster, at 4th a function encapsulates the local variables, such that "clear all" can be omitted directly.
Answers (1)
Yes, your memory is "limited somehow", of course. But the error message tells you unequivocally, that there is a programming error, because you try to get a not existing element of an array.
I guess, that the file contains a line, which does not match the expected pattern.
You can replace the first loop "for i=1:n" by a simpler expression without a loop:
index = (B{5} == 'C');
D{1} = B{2}(index)
D{2} = B{7}(index);
D{3} = B{4}(index);
D{4} = B{9}(index);
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!