1

I have a .txt file with rows consisting of three elements, a word and two numbers, separated by commas.

For example:

a,142,5
aa,3,0
abb,5,0
ability,3,0
about,2,0

I want to read the file and put the words in one variable, the first numbers in another, and the second numbers in another but I am having trouble with textscan.

This is what I have so far:

File = [LOCAL_DIR 'filetoread.txt'];
FID_File = fopen(File,'r');
[words,var1,var2] = textscan(File,'%s %f %f','Delimiter',',');
fclose(FID_File);

I can't seem to figure out how to use a delimiter with textscan.

Shawn
  • 47,241
  • 3
  • 26
  • 60
user3716193
  • 476
  • 5
  • 21
  • 3
    If you're reading a file, then the first argument to `textscan` needs to be a file ID created with [`fopen`](http://www.mathworks.com/help/matlab/ref/fopen.html). Also, your output arguments are invalid. Have you read the documentation? – horchler Feb 08 '16 at 17:38
  • Thanks for the suggestions, I forgot to use fopen(), I edited the post to include it. Still having the same problem though. Horchler, What is meant by "your output arguments are invalid" ? – user3716193 Feb 08 '16 at 17:50
  • 1
    See the answer by @rayryeng below and read the documentation. There is no form of `textscan` that supports more than two output arguments. – horchler Feb 08 '16 at 17:52
  • Got it. I didn't see his answer when I posted that last comment. Thanks. – user3716193 Feb 08 '16 at 17:57

1 Answers1

3

horchler is indeed correct. You first need to open up the file with fopen which provides a file ID / pointer to the actual file. You'd then use this with textscan. Also, you really only need one output variable because each "column" will be placed as a separate column in a cell array once you use textscan. You also need to specify the delimiter to be the , character because that's what is being used to separate between columns. This is done by using the Delimiter option in textscan and you specify the , character as the delimiter character. You'd then close the file after you're done using fclose.

As such, you just do this:

File = [LOCAL_DIR 'filetoread.txt'];
f = fopen(File, 'r');
C = textscan(f, '%s%f%f', 'Delimiter', ',');
fclose(f);

Take note that the formatting string has no spaces because the delimiter flag will take care of that work. Don't add any spaces. C will contain a cell array of columns. Now if you want to split up the columns into separate variables, just access the right cells:

names = C{1};
num1 = C{2};
num2 = C{3};

These are what the variables look like now by putting the text you provided in your post to a file called filetoread.txt:

>> names

names = 

    'a'
    'aa'
    'abb'
    'ability'
    'about'

>> num1

num1 =

   142
     3
     5
     3
     2

>> num2

num2 =

     5
     0
     0
     0
     0

Take note that names is a cell array of names, so accessing the right name is done by simply doing n = names{ii}; where ii is the name you want to access. You'd access the values in the other two variables using the normal indexing notation (i.e. n = num1(ii); or n = num2(ii);).

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Thanks. This seems like it is what I am looking for. I am running into the problem though that using this I end up with an "Empty cell array". Any suggestions? – user3716193 Feb 08 '16 at 17:55
  • @user3716193 Did you use the code exactly as I put it above? Make sure **there are no spaces** in the formatting string (i.e. `%s%f%f` as opposed to `%s %f %f`). – rayryeng Feb 08 '16 at 17:58
  • My mistake, somehow my .txt file was completely blank which is why I was getting an empty cell array. Thanks for the help, this worked. – user3716193 Feb 08 '16 at 18:01
  • @user3716193 oops! No worries. Glad I could help. Good luck! – rayryeng Feb 08 '16 at 18:02