I have a few cvs files from which I want to read specific lines and so to collect specific information from them.
While I found that I am able to read these files all good if removing manually a line, I would like to be able to skip this line using some code to avoid going through each of these files and manually removing this line.
Example:
My file looks like this
- blabla
- blabla
- blabla
- S>
- blabla
- blabla
- nquan = 12
- blabla
I am reading this file using the following code in matlab:
din = 'C:/example/';
CNVfiles = dir ([din '*.cnv']);
fid = fopen([din CNVfiles], 'r');
I want to be able to get the number ’12’ from the line ‘# nquan = 12’ (which is the number of
column (Ncol) that I will need later),
p = ' ';
while ~isequal(p(1:7),'* nquan')
p = fgets(fid);
end
Ncol = str2double(p(11:end));
fclose(fid);
However, it gets me an error stating ‘Index esceed matrix dimension’ at ‘end’ ….when I look at what ‘p’ is, it tells me ‘* S>’ and hence I am guessing that I have an issue when reading that ‘* S>’ line in the files..
When I manually remove that line ‘* S>’, it works all good and I get my Ncol = 12. However, I would like to avoid to do this manually since I have a bunch of cnv files like that.
I was thinking of skipping that line, but do not know how to do that…any ideas what is wrong here? and what can I do to make it works?
Many thanks,
Sophie
You are getting this error because when your loop reaches the line in your file which contains “* S>”, the value of p is equal to
'* S>'. As you can see, p is an array of length 4. When you now tryp(1:7), Matlab complains since you are accessing elements that aren’t present.