Eric
Eric

Reputation: 1

textscan strange behavior

I want to read a text file of the form:

gatcgtacgtcgatgc
gtcgtacgatcgcatg 
cagctgactgatcgatcg 

. So each line is a string, and the delimiter is \n. Suppose this file is called 'data.txt'. I would think

fid = fopen('data.txt')
C = textscan(fid, '%s', 'delimiter', '\n')
fclose('data.txt')

Would give me 3x1 cell with C{1} = 'gatcg....' , C{2} = 'gtcgt....' and C{3} = 'cagctg....'.

This is not the case. It tells me C = {3x1 cell}. However, I get the following behavior:

C{1}

ans = 

   'gatc...'

   'gtcg...'

   'cagc...'

C{2} 
??? Index exceeds matrix dimensions

C{3}
??? Index exceeds matrix dimensions.

What is going on here? o_O

Upvotes: 0

Views: 1451

Answers (2)

Amro
Amro

Reputation: 124553

You should add the following first:

C =  C{1};

%# then you can use
C{1}
C{2}
C{3}

Thats because in your case, TEXTSCAN returns a 1-by-1 cell array C, where its first elements is a three-elements cell array.

Upvotes: 1

b3.
b3.

Reputation: 7155

The output of TEXTSCAN is a cell array which itself contains arrays of the various data inputs. In your case, all of the data is stuffed into a single cell array. To access each entry do this:

>> C = textscan(fid, '%s');
>> C{1}{1}

ans =

gatcgtacgtcgatgc

>> C{1}{2}

ans =

gtcgtacgatcgcatg

>> C{1}{3}

ans =

cagctgactgatcgatcg

Note that you don't need to indicate \n as a delimiter as that is the default end of line character.

Upvotes: 0

Related Questions